xpath选择两个节点之间的元素

时间:2011-10-31 17:44:13

标签: xpath

这是我的HTML:

<table dir = "rtl .......">
<tbody>
<script src = "get.aspx?type=js&file=ajax&rev=3"......>
<script language = "JavaScript" src = "get.aspx?type=js&file=mc&rev=6"></script>
<script>..</script>
<tr>
  <td class = "d2"...>..</td>
</tr>
<tr>..</tr> <--
<tr>..</tr> <--
<tr>..</tr> <-- these elements
<tr>..</tr> <--
<tr>..</tr> <--
<tr>..</tr> <--
<tr>..</tr> <--
<tr>
  <td class = "d2"...>..</td>
</tr>
<script>..</script>
<tr>..</tr>
<tr>..</tr>
<tr>..</tr>

如何计算或选择ID为<tr>的两个<td>元素之间的所有d2元素?

1 个答案:

答案 0 :(得分:3)

xpath将是一个很长的路径,所以支撑自己:

count(//tr[preceding-sibling::tr/td[@class = 'd2']][count(.|//tr[following-sibling::tr/td[@class = 'd2']])=count(//tr[following-sibling::tr/td[@class = 'd2']])])

要选择实际节点而不仅仅是计数,只需删除第一个计数:

//tr[preceding-sibling::tr/td[@class = 'd2']][count(.|//tr[following-sibling::tr/td[@class = 'd2']])=count(//tr[following-sibling::tr/td[@class = 'd2']])]

这里发生了各种各样的事情: