计算XPath中的函数

时间:2009-12-31 13:40:07

标签: xml xpath count

我有一个XML文档,并尝试使用xpath获取具有特定文本的节点数。见下面的xml

count(// event_type)返回event_type节点的数量,但我想要的是 具有错误文本的event_type节点的数量。

  <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>  

谢谢

1 个答案:

答案 0 :(得分:16)

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

应该做的伎俩。此外,XPath的一个方便的工具:http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

希望这有帮助。