I am working with the following (suboptimal) XML:
<a>
<b>
<c>X:1 Y:0</c>
<c>X:1 Y:0</c>
<c>X:2 Y:0</c>
</b>
<b>
<c>X:1 Y:0</c>
<c>X:2 Y:0</c>
</b>
</a>
I am trying to use XPath to count the number of <c>
nodes whose contents contain X:1
:
count(contains(/a/b/c, 'X:1'))
However, this returns an error rather than returning the expected count of 3
.
What am I doing wrong?
That isn't how you use contains()
. Try
count(/a/b/c[contains(., 'X:1')])
Probably a little bit more efficient (if the property exhibited in the provided XML document isn't accidental):
count(/a/b/c[starts-with(., 'X:1')])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With