3 Replies Latest reply on Feb 18, 2012 4:12 PM by tsuji

    xpath to check element exists

    912973
      Hi,

      I wanted to have a xpath expression to find whether the element 'staff' exists or not

      ex:
      <Updates>
      <staff>
      <name>xyz</name>
      </staff>
      <staff>
      <name>xyz</name>
      </staff>
      <staff>
      <name>xyz</name>
      </staff>
      <manager>
      <name>y</name>
      </manager>
      </Updates>


      i wanted to proceed the program logic, if the staff element exists in the xml document. can i have the xpath expression.
        • 1. Re: xpath to check element exists
          Jason_(A_Non)
          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 11.2.0.2.0 - 64bit Production
          from 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.
          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[1]') > 0 THEN
                -- Do stuff since node exists.
                dbms_output.put_line('true');
             END IF;
          END;
          You do not need to use [1], but I included it in case you decide to add a loop and check for the nth occurrence of the staff node.
          • 2. Re: xpath to check element exists
            odie_63
            Well, I guess this simple location path should do it :
            /Updates/staff
            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
            • 3. Re: xpath to check element exists
              tsuji
              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;