ashwnacharya
ashwnacharya

Reputation: 14861

How to query XML nodes based on the value of the attribute of its parent node

I have an XML which looks something like this:

<Library>
<Author Name = "JRR Tolkien">
<Book Title = "Lord Of the Rings" />
<Book Title = "The Hobbit" />
</Author>
<Author Name = "JK Rowling">
<Book Title = "Harry Potter and the Sorcerers Stone" />
<Book Title = "Harry Potter and the Prisoner of Azkaban" />
</Author>
</Library>

What is the XPath to select all the "Book" nodes whose parent "Author" node has the value "JK Rowling"?

Upvotes: 1

Views: 705

Answers (1)

Lasse Espeholt
Lasse Espeholt

Reputation: 17782

This is properly more most intuitive and strict which selects all the books which have a Author which Name attribute is 'JK Rowling' as a parent. And the Author must also have the Library as a parent.

/Library/Author[@Name='JK Rowling']/Book

Upvotes: 3

Related Questions