nimcap
nimcap

Reputation: 10503

How to select nodes that has X as descendant using xpath

consider following example:

<root>
   <instruments>
      <flute>
         <baz>bazik</baz>
      </flute>
      <guitar>
         <deep>
            <baz>more bazik</baz>
         </deep>
      </guitar>
      <drum>
         <foo>fooled</foo>
      </drum>
   </instruments>
</root>

I want to select flute and guitar because they both contain baz as a descendant node. How can I do that?

Upvotes: 15

Views: 6601

Answers (1)

vtd-xml-author
vtd-xml-author

Reputation: 3377

the key is to use predicate [descendant::baz]

so the expression can be

/root/instruments/*[descendant::baz]

Upvotes: 22

Related Questions