Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count function in XPath

Tags:

xml

count

xpath

I have got an XML document and trying to get the number of nodes that have a particular text using xpath. see xml below

count(//event_type) returns the number of event_type nodes but what I want is the number of event_type nodes that have the Error text.

  <Response>
    <run_id>20091231-105000</run_id>
    <message>
      <timestamp>2009-12-31T10:50:00.46875+00:00</timestamp>
      <event_type>Information</event_type>
      <operation>LoadProjects</operation>
      <error_code />
      <details>LoadProjects request detected</details>
    </message>
    <message>
      <timestamp>2009-12-31T10:50:02.296875+00:00</timestamp>
      <event_type>Error</event_type>
      <operation>Processor.InitaliseDCFiles</operation>
      <error_code />
      <details>some error details</details>
    </message>
    <message>
      <timestamp>2009-12-31T10:50:02.296875+00:00</timestamp>
      <event_type>Debug</event_type>
      <operation>Processor.InitaliseDCFiles</operation>
      <error_code />
      <details>some details</details>
    </message>
  <Response>  

Thanks

like image 481
Walid Avatar asked Dec 31 '09 13:12

Walid


1 Answers

count(//event_type[text()='Error']) 

should do the trick. Also, a handy tool for XPath : http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

Hoping this helps.

like image 110
phtrivier Avatar answered Oct 12 '22 07:10

phtrivier