我有一大堆XML数据需要使用XMLTable来获取bulletinWorkl:id
和bulletinWork/outOfServices/outOfService/document:destinationName
。
<?xml version="1.0" encoding="ISO-8859-1"?>
<cern:bulletinWork id="5307cedc-2208-3701-9b9d-e69664b1ef31">
<cern:outOfServices>
<cern:outOfService id="e95d491b-2876-3e08-901f-b0f79be86bfb">
<cern:document destinationName="MonsA" type="S427"></cern:document>
</cern:outOfService>
<cern:outOfService id="fab04992-a33f-3a8c-ad16-29cd54fb93d6">
<cern:document destinationName="MonsB" type="S427"></cern:document>
</cern:outOfService>
</cern:outOfServices>
</cern:bulletinWork>
我正在尝试运行此查询,并成功取回bulletinWork
中的ID属性,但bulletinWork/outOfServices/outOfService/document
中的destinationName为空。
select X.Id, X.DestinationName from S1589_XML_Bulletin, XMLTABLE(
'$d/*:bulletinWork'
PASSING XMLTYPE(XML_RAW) as "d"
COLUMNS
Id VARCHAR2(50) PATH '@*:id',
DestinationName VARCHAR2(50) PATH '*:outOfServices/*:outOfService/*:document/*:destinationName'
) AS X
有人看到我在这里做错了吗? 我需要得到:
Id DestinationName
------------------------------------- ------------------
5307cedc-2208-3701-9b9d-e69664b1ef31 MonsA
5307cedc-2208-3701-9b9d-e69664b1ef31 MonsB
答案 0 :(得分:2)
COLUMNS
Id VARCHAR2(50) PATH '@id',
DestinationName VARCHAR2(50) PATH 'string-join(distinct-values(*:outOfServices/*:outOfService/*:document/@destinationName),", ")'
您不必为未加前缀的属性使用命名空间。它们的名称空间定义为父元素。
在示例xml中有多个cern:outOfService
这就是我使用string-join
和distinct-values
更新:
1)它更长,但对我来说更清晰。两个xml表的连接。
select * from xmltable('*:bulletinWork' passing xmltype('<cern:bulletinWork id="5307cedc-2208-3701-9b9d-e69664b1ef31" xmlns:cern="aaa">
<cern:outOfServices>
<cern:outOfService id="e95d491b-2876-3e08-901f-b0f79be86bfb">
<cern:document destinationName="MonsA" type="S427"></cern:document>
</cern:outOfService>
<cern:outOfService id="fab04992-a33f-3a8c-ad16-29cd54fb93d6">
<cern:document destinationName="MonsB" type="S427"></cern:document>
</cern:outOfService>
</cern:outOfServices>
</cern:bulletinWork>')
COLUMNS
Id VARCHAR2(50) PATH '@id',
outOfServices xmltype path '*:outOfServices'
) t1
,xmltable('*:outOfServices/*:outOfService' passing t1.outOfServices
COLUMNS DestinationName VARCHAR2(50) PATH '*:document/@destinationName')
2)从子节点访问父节点。
select * from xmltable('*:bulletinWork/*:outOfServices/*:outOfService' passing xmltype('<cern:bulletinWork id="5307cedc-2208-3701-9b9d-e69664b1ef31" xmlns:cern="aaa">
<cern:outOfServices>
<cern:outOfService id="e95d491b-2876-3e08-901f-b0f79be86bfb">
<cern:document destinationName="MonsA" type="S427"></cern:document>
</cern:outOfService>
<cern:outOfService id="fab04992-a33f-3a8c-ad16-29cd54fb93d6">
<cern:document destinationName="MonsB" type="S427"></cern:document>
</cern:outOfService>
</cern:outOfServices>
</cern:bulletinWork>')
COLUMNS
Id VARCHAR2(50) PATH './../../@id',
DestinationName VARCHAR2(50) PATH '*:document/@destinationName'
)