Home > OS >  xpath check if specified value exists at least once in each subelement
xpath check if specified value exists at least once in each subelement

Time:10-05

I have this xml.

<collection>
<item>
    <id>001</id>
    <attributes>
        <attr1>A</attr1>
    </attributes>
    <attributes>
        <attr1>B</attr1>
    </attributes>
</item>
<item>
    <id>002</id>
    <attributes>
        <attr1>B</attr1>
    </attributes>
</item>
<item>
    <id>003</id>
    <attributes>
        <attr1>A</attr1>
    </attributes>
</item>
</collection>

I need my xpath query to return me "true" if each item element contains at least one attributes/attr1='A'. I had a go with this xpath as I tried to elaborate on this thread (thank you Mads, you answered the wrong question, my bad)

//item/attributes/attr1/text()='A'

CodePudding user response:

In XPath 2 and later you can literally write every $item in //item satisfies $item/attributes/attr1 = 'A'.

For XPath 1.0 I tend to use a negation, I think: not(//item[not(attributes/attr1 = 'A')]). But I am usually thinking in XPath 3 these days so better let someone who prefers XPath 1 or feels he/she can (still?) juggle with the versions answer that.

  • Related