Oracle XMLTable- 从父节点获取列

Oracle XMLTable- fetching column from parent node

我有以下 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>

预期输出:

  CHILD_ID PARENT_VALUE
---------- ------------
        11 10000            
        12 10000            

我尝试过的:

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;

我的查询有问题,parent_value 变为空。请帮忙。

您正在寻找 ./parent_node,它是 <parent_node> 当前 <child> 节点下。那是不存在的。

你只需要更上一层楼:

parent_value NUMBER (10) PATH './../parent_value'

使用您的 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

我不知道这是最优化的还是最短的版本,但它有效:

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 ('for $c in /root/parent/child 
                          return <child parent_value="{$c/../parent_value}">{$c}</child>'
                       PASSING xcol COLUMNS 
                       child_id NUMBER (5) PATH 'child/child_id',
                       parent_value NUMBER (10) PATH '@parent_value'
                       ) myXmlTable;

我们在 Oracle 12.2.0.1.0 中遇到了同样的问题 - 即 PLSQL 查询未使用 ./../ 语法从 XML 数据中返回父节点值。在我们的例子中,MATERIALIZE 提示导致返回空值 - 不知道为什么,但是当提示被删除时,父节点问题就消失了。