tags:

views:

193

answers:

3
<tr>
 <td>Blah!</td>
 <td>X</td> <!-- TR containing X -->
 <td>Woot!</td>
</tr>
<tr>
 <td>Useful Data, contents unknown</td> <!-- Select this TR -->
</tr>
<tr>
 <td>Useless data</td> <!-- Don't select this or any subsequent TR -->
</tr>
<tr>
 <td>More crap I don't want</td>
</tr>
<tr>
 <td>X</td> <!-- Another X -->
</tr>
<tr>
 <td>Useful</td> <!-- Do select this one, since previous has X -->
</tr>

What XPath would return the <tr> immediately following the <tr> that contains X?

A: 
//td[.='X']/following::tr
Martin v. Löwis
+1  A: 

This should work.

tr[td/text() = 'X']/following-sibling::node()
ChaosPandion
+2  A: 

ChaosPandion's and Martin v. Löwis's answers both work for the sample you give, but if you are asking for the next tr, then presumably in some cases there are further tr elements in same table. In which case, the answers will give all the following or following-sibling tr elements.

Also going by the headline question rather than the sample, the xpath should probably allow for the X being in a th cell instead of td. And I'm guessing that you'd only want the following tr if it is in the same parent (thead, tbody, tfoot).

So I'd go for

//tr[* = 'X']/following-sibling::tr[1]
Alohci