2
votes

So I'm following an example from another question, which I'll replicate here:

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

./book[author/name = '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>

Now, for whatever reason, when I use the xpath above, it DOES select the book node, but it selects all of them, not just the one where author = John. Is there a reason why the xpath would be ignoring that and just pulling every book node?

I hope this makes sense. Thanks!

1
Are you sure it selects all book nodes? Your xpath looks ok - Aleh Douhi
@AlehDouhi Admittedly, I'm working with xpath through a Ruby gem that maps XML to Ruby objects. However, the documentation is slim, and it doesn't seem to be very widespread. I was hoping The XML example was missing something, but I think you're right. That XPath should work, but in the context of my Ruby application it's just grabbing everything. Sigh. - librarion

1 Answers

2
votes

The following should work:

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

or

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