db2检索xml元素的多个示例

gajydyqb  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(355)

我对查询xml是个新手。
我有一个在属性内容中包含xml字符串的通知表。属性的内容如下(格式化):

<?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可以出现多次
在db2中,我希望输出为:

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

我有以下问题

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

不幸的是,这没有输出。
有人能帮我解答这个问题吗?

wgxvkvu9

wgxvkvu91#

试试这个:

/*
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;

相关问题