xpath 选择两个节点之间的元素 [英] xpath select elements between two nodes

查看:34
本文介绍了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 为 d2?

How would I count or select all <tr> elements between the two <td> elements whose id is d2?

推荐答案

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']])])

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

To select the actual nodes and not have just the count, simply remove the first count :

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

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

There are various things happening here notably:

  • Select start node by selecting preceding sibling's child node tr/td with id='2'
  • Select end node by selecting following sibling's child node tr/td with id='2'
  • Use kaycian method : http://www.dpawson.co.uk/xsl/sect2/muench.html#d9940e108 to get the intersection between the two nodes.

这篇关于xpath 选择两个节点之间的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆