xmlxpathxpath-2.0

XPath that returns true when at least one element matches


Let's assume we have the following XML response:

<People>
    <Person>
        <Age>29</Age>
    </Person>
    <Person>
        <Age>25</Age>
    </Person>
    <Person>
        <Age>18</Age>
    </Person>
    <Person>
        <Age>45</Age>
    </Person>
</People>

I want an xpath 2.0 expression that will return true if there is at least one person with age between 18 and 22.

My current expression is:

boolean(//*:Person[xs:integer(substring(//*[local-name() = 'Age']/text(), 2)) >= 18 and 22 >= xs:integer(substring(//*[local-name() = 'Age']/text(), 2))])

But this expression is not recursive so it produces the following error:

A sequence of more than one item is not allowed as the first argument of substring() ("29", "25", ...)

Any idea as to how I can achieve what I need?


Solution

  • In XPath 2.0 this is exists(//Person[Age = (18 to 22)])