yegor256
yegor256

Reputation: 105213

How to refer to the parent in XPath?

I'm trying to do this:

//x[//y[@a = @b]]

I need to find all elements <x>, which have <y> somewhere in the document, such that x/@a = y/@b. Obviously, my code is wrong. How to show that @a belongs to x and @b belongs to y?

Upvotes: 1

Views: 155

Answers (1)

kjhughes
kjhughes

Reputation: 111726

Regarding the literal title question (How to refer to the parent in XPath?), the answer would be to use the parent:: axis or its abbreviation (..). However, that's not actually needed to achieve the requested outcome here.

This XPath,

//x[@a = //y/@b]

will select all x elements with an a attribute value that equals some y element's b attribute value anywhere in the document.

Upvotes: 3

Related Questions