Search code examples
xpathsubtree

Xpath - How to select subnode where sibling-node contains certain text


I want to use XPath to select the sub tree containing the <name>-tag with "ABC" and not the other one from the following xml. Is this possible? And as a minor question, which keywords would I use to find something like that over Google (e.g. for selecting the sub tree by an attribute I would have the terminology for)?

<root>
  <operation>
    <name>ABC</name>
    <description>Description 1</description>
  </operation>
  <operation>
    <name>DEF</name>
    <description>Description 2</description>
  </operation>
</root>

Solution

  • Use:

    /*/operation[name='ABC']
    

    For your second question: I strongly recommend not to rely on online sources (there are some that aren't so good) but to read a good book on XPath.

    See some resources listed here: https://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589