Kyle
Kyle

Reputation: 4366

XPath selector for element with specified attribute value and has descendant with specified attribute value

I'm looking for an XPath selector which will get an element with a specified attribute value and also a descendant with a specified attribute value. The following is an example:

<book type="nonfiction">
    <title>foo</title>
    <author>
        <name>John Doe</name>
        <dob>1/1/1900</dob>
    </author>
</book>
<book type="fiction">
    <title>bar</title>
    <author>
        <name>Jane Doe</name>
        <dob>2/2/2000</dob>
    </author>
</book>

So in this example I'd like to get all the <book> elements with a type of nonfiction who have an <author> with a dob of 1/1/1900. I don't know how many levels deep the <author> node is though so I need a solution that can check all descendants.

Upvotes: 0

Views: 82

Answers (2)

har07
har07

Reputation: 89285

You can try this way :

//book[@type='nonfiction' and .//author[dob='1/1/1900']]

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118271

You can use the below to get the desired output:

//book[@type = "nonfiction"][.//author[dob = "1/1/1900"]]

Upvotes: 0

Related Questions