Rac*_*cha 4 oracle xpath xmltype xmltable oracle-xml-db
我有以下 XML 结构:
<root>
<parent>
<parent_id>1</parent_id>
<parent_value>10000</parent_value>
<child>
<child_id>11</child_id>
<other_value>1000</other_value>
</child>
<child>
<child_id>12</child_id>
<other_value>1000</other_value>
</child>
</parent>
</root>
Run Code Online (Sandbox Code Playgroud)
预期输出:
CHILD_ID PARENT_VALUE
---------- ------------
11 10000
12 10000
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
WITH xtbl AS (SELECT xmltype ('<root>
<parent>
<parent_id>1</parent_id>
<parent_value>10000</parent_value>
<child>
<child_id>11</child_id>
<other_value>1000</other_value>
</child>
<child>
<child_id>12</child_id>
<other_value>1000</other_value>
</child>
</parent>
</root>') AS xcol FROM dual)
SELECT myXmlTable.*
FROM xtbl
CROSS JOIN
xmltable ('/root/parent/child'
PASSING xcol
COLUMNS child_id NUMBER (5) PATH 'child_id',
parent_value NUMBER (10) PATH './parent_value') myXmlTable;
Run Code Online (Sandbox Code Playgroud)
我的查询的问题是它parent_value为空。请帮忙。
您正在寻找./parent_node,这是当前节点<parent_node> 下的一个<child>。而那是不存在的。
你只需要提升一个层次:
parent_value NUMBER (10) PATH './../parent_value'
Run Code Online (Sandbox Code Playgroud)
使用您的 CTE 进行演示,并添加../:
WITH xtbl AS (SELECT xmltype ('<root>
<parent>
<parent_id>1</parent_id>
<parent_value>10000</parent_value>
<child>
<child_id>11</child_id>
<other_value>1000</other_value>
</child>
<child>
<child_id>12</child_id>
<other_value>1000</other_value>
</child>
</parent>
</root>') AS xcol FROM dual)
SELECT myXmlTable.*
FROM xtbl
CROSS JOIN
xmltable ('/root/parent/child'
PASSING xcol
COLUMNS child_id NUMBER (5) PATH 'child_id',
parent_value NUMBER (10) PATH './../parent_value') myXmlTable;
CHILD_ID PARENT_VALUE
---------- ------------
11 10000
12 10000
Run Code Online (Sandbox Code Playgroud)