7gegenTheben
7gegenTheben

Reputation: 81

Xpath how to use and statement and parent condition on a node selection

Suppose I have a XML like this:

<my-parent-tag attr="">
  <my-child-tag/>
</my-parent-tag>
<my-child-tag/> <!--I don't want to select this one-->

I want to find all my-child-tags that have the parent node my-parent-tag with attr="". How do I express this in Xpath? I tried

//my-child-tag and [../my-parent-tag[@attr='']]

But, I ended up getting Xpath-no-node-selected feedback. Help appreciated :)

Upvotes: 0

Views: 94

Answers (3)

lauda
lauda

Reputation: 4173

Why not just use regular expression //parent/child

Selects all descendant tags from parent with attribute:

//my-parent-tag[@attr='']//my-child-tag

Selects all child tags from parent with attribute:

//my-parent-tag[@attr='']/my-child-tag

Upvotes: 2

kjhughes
kjhughes

Reputation: 111678

This XPath,

//my-parent-tag[@attr='']/my-child-tag

will select all my-child-tag elements with a parent of my-parent-tag that has a attr attribute value of ''.

Upvotes: 0

Amrendra Kumar
Amrendra Kumar

Reputation: 1816

You can use //my-child-tag[parent::my-parent-tag[@attr='']]

Upvotes: 0

Related Questions