Re: Counting Elements in an xml file



On 30 Aug, 19:37, Ouray Viney <ovi...@xxxxxxxxx> wrote:

<Testcase execute="true" name="foobar">

I would like to be able to count the number of TestCases that contain
the "execute=true" but not the ones that contain "execute=false".

With XPath-capable libraries, it should be enough to execute an XPath
query on the document. For example:

import libxml2dom
d = libxml2dom.parse(filename)
number_of_cases = d.xpath("count(//Testcase[@execute='true'])")

This applies the XPath count function to all Testcase elements in the
document having an execute attribute with a value of 'true', thus
returning the number of matching elements.

Paul
.



Relevant Pages

  • Re: Counting Elements in an xml file
    ... With XPath-capable libraries, it should be enough to execute an XPath ...   import libxml2dom ... This applies the XPath count function to all Testcase elements in the ...
    (comp.lang.python)
  • Re: XPath Comparisons
    ... when I try to execute ... the expression work in Stylus Studio Pro 2009) but a similar ... With XPath 2.0 you can do such comparisons for other types including xsd:date and xsd:dateTime, however the xsd:date format is yyyy-mm-dd. ...
    (comp.text.xml)
  • Good XPath API for Java?
    ... I am trying to learn XPath and would therefore like to be able to execute various XPath statements on different XML files. ...
    (comp.text.xml)