Using XPath count () with contains ()

I work 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 nodes <c>whose contents contain X:1:

count(contains(/a/b/c, 'X:1'))

However, this returns an error, and does not return the expected quantity 3.

What am I doing wrong?

+5
source share
2 answers

This is not how you use it contains(). Try

count(/a/b/c[contains(., 'X:1')])
+8
source

Probably a bit more efficient (if the property presented in the provided XML document is not random):

count(/a/b/c[starts-with(., 'X:1')])
+3
source

All Articles