Exocom
Exocom

Reputation: 791

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>

Upvotes: 1

Views: 1646

Answers (2)

Bruce Sun
Bruce Sun

Reputation: 661

For your first question, I think a more accurate way to do it would be:
//operation[./name[text()='ABC']].
And according to this , we can also make it:
//operation[./name[text()[.='ABC']]]

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

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

Upvotes: 1

Related Questions