I am testing some xpath expressions using http://www.freeformatter.com/xpath-tester.html (xpath 1.0)
I have this xml code
<urls>
<url uri="http://example.com/pingtest.html">
<elements>
<element title="header" xpath="My header"></element>
</elements>
</url>
<url uri="http://example.com/othertest.html">
<elements>
<element title="header3" xpath="Third header"/>
<element title="header2" xpath="Second header"/>
</elements>
</url>
</urls>
Usually when I query xpath with //element/@title I actually get
Attribute='title="header"' Attribute='title="header3"' Attribute='title="header2"'
What I'm looking for is a way to get a count of elements by url. It could be something like this. When I query:
//urls/url/elements/count(element)
I should get (EDIT: as @Ian Roberts said, this query is valid in xpath 2.0):
Double='1.0' Double='2.0'
Is there a way to do this in a single xpath expression?
Some failing examples:
This returns the count of all element 3 (is not what I'm looking for)
count(//urls/url/elements/element)
This returns the count of all elements list 2 (neither what I'm looking for)
count(//urls/url/elements)
I need this because I need to know how many elements does the xml have for each url, so I can use this count afterwards
//urls/url/elements/count(element)
is a single XPath expression that returns a sequence of numbers representing the number ofelement
elements under eachurl
. – Ian Roberts