Mark Hizzle
Mark Hizzle

Reputation: 55

xpath get child node excluding parent

I'm looking for an xpath that will give me a child node only if the parent node doesn't equal a specific value. For example if I have an xml like the following:

<Grandpa><Dad><Son /></Dad><Son /></Grandpa>

I want to return the Son element outside the Dad element.

Upvotes: 2

Views: 790

Answers (1)

kjhughes
kjhughes

Reputation: 111726

This Xpath selects those Son elements whose parent element is not named Dad:

//Son[local-name(..) != 'Dad']

So, applied to this XML:

<Grandpa><Dad><Son a="1"/></Dad><Son a="2"/></Grandpa>

It will select:

<Son a="2"/>

Upvotes: 1

Related Questions