wolfgang
wolfgang

Reputation: 7819

XPATH - how to query node based on sibling attributes/data?

...More <book></book> objects here

<book>
  <author>John</author>
  <price>50</price>
</book>

<book>
  <author>Henry</author>
  <price>60</price>
</book>
... More <book></book> objects here

I want to get the price value of the book with author John

This is my failed attempt //book//[self::author//text()="john"].//price

What is the right query to get this price detail?

Upvotes: 0

Views: 79

Answers (2)

Kachna
Kachna

Reputation: 2961

try this way:

//book[author= 'John']/price/text()

this will return : 50

I f you want to get the price element, you should use the following expression:

//book[author= 'John']/price

it returns : <price>50</price>

notice that XML is case sensitive. so, john and John are different values.

Upvotes: 5

Scott Johnson
Scott Johnson

Reputation: 369

I used this site to test some xpaths out. Xpath Tester seems //book//price worked fine to return the price.

Upvotes: -2

Related Questions