Glenn
Glenn

Reputation: 43

XPath to identify certain groups of children

Let's say I have some XML like this:

<group>
  <request>
    <field id="a"/>
    <field id="b"/>
    <field id="c"/>
  </request>
  <request>
    <field id="a"/>
    <field id="b"/>
    <field id="d"/>
  </request>
</group>

Note that the order of the <request> elements is not guaranteed.

I want to come up with two XPath expressions, the first to check that a <request> with children a,b and c exists, and the second that a <request> with children a,b and d exists.

How do I specify this? The closest I've come uses the numbered predicate syntax, but I'm missing something on how to specify multiple child matches, and the fact that the order of the <request> elements is not defined is also tripping me up.

Thanks in advance

Glenn.

Upvotes: 4

Views: 92

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

I want to come up with two XPath expressions, the first to check that a <request> with children a,b and c exists, and the second that a <request> with children a,b and d exists

Use:

boolean(/*/request[field[@id='a'] and field[@id='b'] and field[@id='c']])

For the second expression use:

boolean(/*/request[field[@id='a'] and field[@id='b'] and field[@id='d']])

Upvotes: 2

emma
emma

Reputation: 68

Isn't this what you are looking for ?

request[field[@id = 'a'] and field[@id='b'] and field[@id='c']]

?

Upvotes: 1

Related Questions