简体   繁体   中英

DB2 retrieving multiple occurrences of a xml element

I am new to querying xml.

I have a table NOTIFICATIONS that contains a xml string in the attribute CONTENT. The content of the attribute looks like this (formated):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<interuptionLimitation xmlns="http://example.com/schema">
    <person>1000000</person>
    <date>2020-07-20</date>
    <reason>Contact</reason>
    <debtId>1</debtId>
    <debtId>2</debtId>
</interuptionLimitation>

debId can occur multiple times

In DB2 I would like the output as:

person  date       reason debtId
1000000 2020-07-20 Contact 1
1000000 2020-07-20 Contact 2

I have the following query

SELECT X.*
FROM NOTIFICATIONS n
 , XMLTABLE ('$I/interuptionLimitation ' PASSING XMLPARSE(document n.CONTENT) as "I"
  COLUMN
  "person" bigint PATH 'person',
  "date"  CHAR(10) PATH 'date',
  "reason" VARCHAR(200) PATH 'reason',
  "debtId" bigint  PATH '@debtId') AS X

Unfortunately this gives no output.

Can someone help me with this query?

Try this:

/*
WITH NOTIFICATIONS (CONTENT) AS 
(
VALUES '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<interuptionLimitation xmlns="http://example.com/schema">
    <person>1000000</person>
    <date>2020-07-20</date>
    <reason>Contact</reason>
    <debtId>1</debtId>
    <debtId>2</debtId>
</interuptionLimitation>'
)
*/
SELECT X.*
FROM NOTIFICATIONS n
, XMLTABLE 
(
XMLNAMESPACES(DEFAULT 'http://example.com/schema'),
'$I/interuptionLimitation/debtId' PASSING XMLPARSE(document n.CONTENT) as "I"
  COLUMNS
  "person" bigint       PATH '../person',
  "date"   CHAR(10)     PATH '../date',
  "reason" VARCHAR(200) PATH '../reason',
  "debtId" bigint       PATH '.'
) AS X;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM