2014-10-02 69 views
1

如何检查给定节点是否存在于XML文档中?如何检查给定节点是否存在于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

回答

2

您可以简单地使用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; 
相关问题