【问题标题】:Oracle XMLTable- fetching column from parent nodeOracle XMLTable-从父节点获取列
【发布时间】:2018-12-23 23:04:33
【问题描述】:

我有以下 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 为空。请帮忙。

【问题讨论】:

    标签: oracle xpath xmltype xmltable oracle-xml-db


    【解决方案1】:

    您正在寻找./parent_node,它是一个&lt;parent_node&gt;当前&lt;child&gt; 节点下。那是不存在的。

    你只需要升一级:

    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
    

    【讨论】:

    • 奇怪,因为它不起作用。 parent_value 为 NULL。 更新:它在 11.2.0.3.0 数据库上不起作用,在 11.2.0.4.0 上它可以正常工作。
    • 这适用于 11.2.0.4、12.1.0.2 和 12.2.0.1。但它在 11.2.0.2 (dbfiddle) 中确实为 null,我猜这是该版本中的一个错误。 (这也不适用于/ancestor::node()[1]/parent_value,这是我的备份......)
    • 可以LEFT (or RIGHT depends) OUTER JOIN 帮忙吗?在我的情况下确实如此。
    【解决方案2】:

    我不知道这是最优化还是最短的版本,但它可以工作:

    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;
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-14
        • 2012-02-04
        • 2015-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多