I tweaked this for my environment, and it worked perfectly!! This is exactly what I was looking for,
Original Message:
Sent: Thu May 29, 2025 11:16 AM
From: Mark Barinstein
Subject: Getting a list of XML Fields in an XML column
This shouldn't work - `XMLCOL` is not passed from `mytable`.
Below is working as-is example with `name`, `parent_name` for each table element allowing you understand the document structure.
select v.*
from
table(MON_GET_DATABASE_DETAILS (-2)) p
--table (values '<doc docattr="f"><a1 elattr="2019-01-01">1</a1></doc>' ) p(details)
, XMLTABLE (
--XMLNAMESPACES(DEFAULT 'http://www.ibm.com/xmlns/prod/db2/mon'),
'$D//(*, @*)'
PASSING XMLPARSE(DOCUMENT p.DETAILS) AS "D" COLUMNS
ename VARCHAR(50) PATH 'name(.)'
, p_ename VARCHAR(50) PATH 'name(..)'
, type VARCHAR(10) PATH 'if (self::element()) then (if (./*) then (''NON-LEAF'') else (''LEAF'')) else (''ATTRIBUTE'')'
, val_str VARCHAR(50) PATH 'if (./*) then () else .'
, val_int BIGINT PATH 'if (. castable as xs:long) then xs:long(.) else ()'
, val_ts TIMESTAMP PATH 'if (. castable as xs:dateTime) then xs:dateTime(.) else ()'
, val_dt DATE PATH 'if (. castable as xs:date) then xs:date(.) else ()'
) v
You get something like this:
|ENAME |P_ENAME |TYPE |VAL_STR |VAL_INT |VAL_TS |VAL_DT|
|-------------------|------------|---------|--------------------------|----------|--------------------------|------|
|db2_database | |NON-LEAF | | | | |
|release |db2_database|ATTRIBUTE|11050700 |11,050,700| | |
|member |db2_database|LEAF |0 |0 | | |
|db_status |db2_database|LEAF |ACTIVE | | | |
|db_activation_state|db2_database|LEAF |EXPLICIT | | | |
|db_conn_time |db2_database|LEAF |2025-05-29T14:33:13.443404| |2025-05-29-14.33.13.443404| |
|catalog_partition |db2_database|LEAF |0 |0 | | |
|last_backup |db2_database|LEAF |2025-01-29T13:26:06.000000| |2025-01-29-13.26.06.000000| |
...
------------------------------
Mark Barinstein
Original Message:
Sent: Thu October 12, 2023 08:17 AM
From: Philip Nelson
Subject: Getting a list of XML Fields in an XML column
Douglas,
The key to doing this are a set of XML functions which return the names of nodes in an XML document.
Here's an example of what you can do, based on an example in the pureXML cookbook -
SELECT T.*
FROM mytable
XMLTABLE('$XMLCOL//(*, @*)'
COLUMNS
seq FOR ORDINALITY,
node VARCHAR(20) PATH 'local-name(.)',
type VARCHAR(15) PATH
'if (self::attribute())
then "Attribute"
else (if (./*)
then "Element"
else "Leaf-Element")'
) AS T
;
The local-name() function is the secret sauce here. There are a bunch of functions that give you details of the various pieces of the hierarchy. I will try to find some time to write a more comprehensive example later.
Incidentally the Db2 pureXML Cookbook is the go-to resource for all questions pureXML.
------------------------------
Philip Nelson
Original Message:
Sent: Wed October 11, 2023 04:41 PM
From: Douglas Kostelnik
Subject: Getting a list of XML Fields in an XML column
Hi,
I started a new job a couple months back. The company has a number of tables (<10) that have an XML column in them that people have been using for a variety of reasons. What they want to do is identify all of the fields in each row (per table) and count how many times those fields appear in the XML column in the table - basically doing an inventory and count of the XML fields in each table. Each row can have different fields in the column.
The end goal is to get a list of all of the fields (and number of times they exist) per table, and determine which ones are valid. Then add those fields as columns to the table, populate the new columns and remove the XML column completely.
The part I need help on is the Inventory and Counting of the individual fields. Does anyone have a process or method that does this?
------------------------------
Douglas Kostelnik
------------------------------