Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select nodes that has X as descendant using xpath

Tags:

xml

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?

like image 841
nimcap Avatar asked Feb 23 '10 21:02

nimcap


People also ask

What is the meaning of '/' in XPath?

0 votes. Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

Can you use XPath expression used to select the target node and its values?

XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML. Step 1 − After clicking Add Assertion, select Assertion Category – Property Content.


1 Answers

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

so the expression can be

/root/instruments/*[descendant::baz]
like image 51
vtd-xml-author Avatar answered Sep 22 '22 18:09

vtd-xml-author