XPath to select element based on childs child value

XmlXpath

Xml Problem Overview


Trying to select an element based on the value of one of it's childrens childrens

Thinking the following but not working, appreciate any help, thanks

./book[/author/name = 'John'] or

./book[/author/name text() = 'John']

Want all books where the author name = 'John'

Xml file

<list>
   <book>
      <author>
         <name>John</name>
         <number>4324234</number>
      </author>
      <title>New Book</title>
      <isbn>dsdaassda</isbn>
   </book>
   <book>...</book>
   <book>...</book>
</list>

Xml Solutions


Solution 1 - Xml

Almost there. In your predicate, you want a relative path, so change

./book[/author/name = 'John'] 

to either

./book[author/name = 'John'] 

or

./book[./author/name = 'John'] 

and you will match your element. Your current predicate goes back to the root of the document to look for an author.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames WalshView Question on Stackoverflow
Solution 1 - XmlAakashMView Answer on Stackoverflow