【问题标题】:How to check if a given node exists in XML document如何检查给定节点是否存在于 XML 文档中
【发布时间】:2014-10-02 10:53:32
【问题描述】:

如何检查给定节点是否存在于 XML 文档中?

XML 示例:

<order>
    <desc>
        <name>Test name</name>
        <code>Test code</code>
    </desc>
    <suborders>
        <item>
            <id>1000</id>
        </item>
        <item>
            <id>2000</id>
        </item>
    </suborders>
    <options>
       <item/>
    </options>
</order>

如果存在任何子订单的项目,如何检查PL/SQL?

我试过这样:

DECLARE
   myxml CLOB := ...
BEGIN 
   SELECT extractValue(XMLTYPE(myxml), '/order/suborders/item[1]') 
     INTO firstSuborder 
    FROM DUAL;

   IF (firstSuborder IS NULL) THEN
          dbms_output.put_line('suborder doesnt exist');
   END IF;
END;

但我得到了ORA-19025: EXTRACTVALUE returns value of only one node

【问题讨论】:

    标签: sql xml oracle plsql


    【解决方案1】:

    你可以简单地使用existsnode,这里是文档http://docs.oracle.com/cd/B19306_01/appdev.102/b14259/xdb04cre.htm#i1032763

    declare
      OBJECT_VALUE xmltype := xmltype(q'{<order>
        <desc>
            <name>Test name</name>
            <code>Test code</code>
        </desc>
        <suborders>
            <item>
                <id>1000</id>
            </item>
            <item>
                <id>2000</id>
            </item>
        </suborders>
        <options>
           <item/>
        </options>
    </order>}');
    
      node_exists pls_integer;
    
    begin
    
      select existsNode(OBJECT_VALUE, '/order/suborders/item[1]') into node_exists from dual;
      dbms_output.put_line('exists: ' || to_char(node_exists));
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 2016-08-29
      • 2013-03-26
      • 2017-04-04
      • 1970-01-01
      相关资源
      最近更新 更多