0

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'
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147

1 Answers1

3

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • And what if I wanted to achieve this in xpath 1.0? – James Taylor Oct 03 '22 at 16:47
  • @JamesTaylor, I tend to use a negation for XPath 1.0 but I am not used to think and express things anymore in that version. See the edit and let's wait whether someone objects or has better idea. – Martin Honnen Oct 03 '22 at 17:54
  • @MartinHonnen I checked your answer `not(//item[not(attributes/attr1 = 'A')])` on http://xpather.com/. It works perfect. – Prophet Oct 03 '22 at 21:31