在Oracle 11g中,如果XML文档中没有父标记的嵌套标记,则结果集中不会选择父标记值
我有嵌套的xml文档。但有些地方没有筑巢。我试图获取所有要插入表中的信息。但如果没有嵌套(子标记),则查询不会返回父标记值。如果不存在嵌套,我希望在具有父标记值的子标记处返回null值。以下是 在表中创建数据的代码在Oracle 11g中,如果XML文档中没有父标记的嵌套标记,则结果集中不会选择父标记值,xml,oracle,Xml,Oracle,我有嵌套的xml文档。但有些地方没有筑巢。我试图获取所有要插入表中的信息。但如果没有嵌套(子标记),则查询不会返回父标记值。如果不存在嵌套,我希望在具有父标记值的子标记处返回null值。以下是 在表中创建数据的代码 create table xmldata as with xmldoc as ( select xmlType(' <detailedOutput> <entity> <entityLabel>Parent
create table xmldata as
with xmldoc as (
select xmlType('
<detailedOutput>
<entity>
<entityLabel>Parent tag with child</entityLabel>
<entityName>P with C</entityName>
<entityType>P with C</entityType>
<fact>
<name>I am child 1</name>
</fact>
<fact>
<name>I am child 2</name>
</fact>
</entity>
<entity>
<entityLabel>Parent tag with no child</entityLabel>
<entityName>P with no C</entityName>
<entityType>P with no C</entityType>
</entity>
</detailedOutput>
') xml from dual
)
select xml from xmldoc
这里我得到以下结果:
此处未选择entityLabel字段无子项的父标记的值。请帮助我如何对提到的xml文档进行排序,以便在没有子标记的情况下选择子值为null的父标记值。这是因为您正在
f
上执行内部联接,该联接将删除所有没有事实/名称子项的实体。试试这个
select
extractValue (value(e), '/entity/entityLabel') as "entityLabel",
extractValue (value(e), '/entity/entityName') as "entityName",
extractValue (value(e), '/entity/entityType') as "entityType",
extractValue (value(f), '/fact/name') as "factName"
from xmldoc x
inner join table (xmlSequence(extract(xml, '/detailedOutput/entity'))) e
on 1=1
left join table (xmlSequence(extract(value(e), '/entity/fact'))) f
on value(e) is not null
我不确定为什么f
需要一个连接条件来连接e
,但是没有它它就不能正常工作 所以XML中逗号分隔的表是内部联接,而不是常规ANSI-SQL中的交叉联接?我在工作中遇到了一个问题。我没有尝试运行解释计划,但我认为表(xmlSequence())
在幕后添加了一个隐式连接条件。我认为它不可能是交叉连接,因为伪表显然不是相互独立的。
select
extractValue (value(e), '/entity/entityLabel') as "entityLabel",
extractValue (value(e), '/entity/entityName') as "entityName",
extractValue (value(e), '/entity/entityType') as "entityType",
extractValue (value(f), '/fact/name') as "factName"
from xmldoc x
inner join table (xmlSequence(extract(xml, '/detailedOutput/entity'))) e
on 1=1
left join table (xmlSequence(extract(value(e), '/entity/fact'))) f
on value(e) is not null