Reputation: 1431
I am trying to create an xpath expression that will find the first matching sibling 'down' the dom given an initial sibling (note: initial siblings will be Tom and Steve). For example, I want to find 'jerry1' under the 'Tom' tr. I have looked into the following-sibling argument, but I'm not sure that's the best approach for this? Any ideas?
<tr>
<a title=”Tom”/>
</tr>
<tr>
<a title=”jerry1”/>
</tr>
<tr>
<a title=”jerry2”/>
</tr>
<tr>
<a title=”jerry3”/>
</tr>
<tr>
<a title=”Steve”/>
</tr>
<tr>
<a title=”jerry1”/>
</tr>
<tr>
<a title=”jerry2”/>
</tr>
<tr>
<a title=”jerry3”/>
</tr>
Upvotes: 2
Views: 216
Reputation: 66723
The following XPath statement finds the first tr
element that has an a
with the @title
"jerry1" that is a following-sibling of the tr
element that has an a
with the @title
of "Tom"
//tr[a/@title='Tom']/following-sibling::tr[a/@title='jerry1'][1]
Upvotes: 0
Reputation: 11416
Following XPath worked for me:
(//a[@title='Tom']/parent::*/following-sibling::tr/a[@title= 'jerry1'])[1]
First matching a
with title
jerry1 following a tr
with an a
-child with title
Tom.
Starting at a[@title='Tom']
, going to the parent tr with /parent
, selecting all following sibling tr-nodes with ::*/following-sibling::tr
, that have an /a[@title= 'jerry1']
as child node. Because this would select 2 jerry1
-nodes and the first jerry1 following Tom is searched, selecting the first one by wrapping the XPath with ()
and choosing the first match with [1]
.
Upvotes: 0
Reputation: 3110
following-sibling
will work. This will select the a
node with the title "jerry1":
//a[@title='Tom']/../following-sibling::tr/a
The /..
traverses up to Tom's parent <tr>
, then following-sibling
to the next <tr>
, then finally the <a>
node within that.
Upvotes: 1