Reputation: 2105
Here is the xml:
<cat> <row> <col attr="w">111</col> <col attr="p">222</col> <col attr="g">333</col> <col attr="r">444</col> <col attr="n1">555</col> <col attr="n2">666</col> <col attr="s">777</col> </row> <row> <col attr="w">aaa</col> <col attr="p">bbb</col> <col attr="g">nnn</col> <col attr="r">mmm</col> <col attr="n1">xxx</col> <col attr="n2">ccc</col> <col attr="s">vvv</col> </row> </cat>
What I look for: find
//cat/row/col[@attr='n2']
only , if in the same parent node
//cat/row/col[@attr='w']==111
Upvotes: 0
Views: 181
Reputation: 1482
This xpath will find the col with attr=w and text='111', then go back to it's parent and look for the parent's children with col @attr=n2
//row/col[@attr='w' and text()='111']/../col[@attr='n2']
Upvotes: 0
Reputation: 5114
Here's an alternative solution which I consider more readable as you add the conditions in each node of the XPath expression as you go, without the need to use parent
:
//cat/row[./col[@attr='w' and text()='111']]/col[@attr='n2']
with the same output:
<col attr="n2">666</col>
Upvotes: 1
Reputation: 3138
Your correct XPATH is:
//cat/row/col[@attr='n2'][parent::row/col[@attr='w' and text()='111'] ]
to get output:
<col attr="n2">666</col>
Upvotes: 1