This content has been marked as final. Show 3 replies
Details about where the XML resides, what approach you are trying or need to use, and your version of Oracle is always helpful.
It looks like you are on
Oracle Database 11g Enterprise Edition Release 22.214.171.124.0 - 64bit Productionfrom a previous post I see.
Here's a shot in the dark assuming that the XML is stored in a XMLType variable and you want to use PL/SQL to parse the XML and make that decision for.
You do not need to use , but I included it in case you decide to add a loop and check for the nth occurrence of the staff node.
DECLARE l_xmltype XMLTYPE; BEGIN l_xmltype := XMLType('<Updates> <staff> <name>xyz</name> </staff> <staff> <name>xyz</name> </staff> <staff> <name>xyz</name> </staff> <manager> <name>y</name> </manager> </Updates>'); -- Look to see if a staff node exists IF l_xmltype.Existsnode('/Updates/staff') > 0 THEN -- Do stuff since node exists. dbms_output.put_line('true'); END IF; END;
Well, I guess this simple location path should do it :
But it may depend on the method/function you're using to actually perform the test.
If I refer to your previous posts, you're working in the database (if it's not the case here, please explain the context).
For example :
SQL> with t as ( 2 select xmltype('<Updates> 3 <staff><name>xyz</name></staff> 4 <manager><name>y</name></manager> 5 </Updates>') doc 6 from dual 7 ) 8 select case when xmlexists('/Updates/staff' passing t.doc) then 'YES' end as "exists staff?" 9 from t 10 ; exists staff? --------------- YES
If it is in a plain java code, that suppose xp be the XPath instance set up, that doc be the Document instance loaded, you can use an xpath (sure there is no unique/best choice) like this for illustration.
//xp be the XPath instance, doc be the Document instance loaded up String sxpr="count(/Updates/staff)"; Double ncount=(Double)xp.evaluate(sxpr,doc,XPathConstants.NUMBER); boolean bExists=(ncount.intValue()>0)?true:false;