XPath:如何选择以下兄弟姐妹直到某个兄弟姐妹 [英] XPath: how to select following siblings until a certain sibling

查看:183
本文介绍了XPath:如何选择以下兄弟姐妹直到某个兄弟姐妹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下面的示例HTML,返回具有class ='B'的class ='A'的a元素的同胞的XPath查询可以写为: // a [ @类= 'A'] /以下同胞::一个[@类= 'B'] 。这个查询输出4 < a class =B/> 元素。 然而,我只会喜欢< a class =B/> 元素。当前< a class =A/> 元素,并且没有其他人遵循其他< a class =B/> 元素/节点。换句话说,我只需要下面的< a class =B/> 兄弟元素,直到下一个< a class = B $ / b $ / b







$ p> < a class ='A'/>
< a class ='B'/>
< a class ='A'/>
< a class ='B'/>
< a class ='B'/>
< a class ='B'/>

有关如何将当前XPath查询限制为仅适用于这些兄弟的任何想法都将非常感谢:)

解决方案

要选择所有 a 在 a 类之间的属性> B 等于 A ,接下来会出现这样的情况:

 <$ c $ b $ class ='A'] [$ n] / following-sibling :: a [
@ class ='B'and count(preceding-sibling :: a [@ class = 'A'])= $ n]

选择 n a [@ class ='A'] 和下一个这样的元素。对于一个具体的例子,请考虑以下输入:

 < r> 
< a class =A/>
< a class =B/>
< a class =A/>
< a class =B/>
< a class =B/>
< a class =A/>
< a class =B/>
< a class =B/>
< a class =B/>
< / r>

获取第二个< a class =A /> 和第三个< a class =A/>

  / * / a [@ class ='A'] [2] / following-sibling :: a [
@ class ='B'and count( -sibling :: a [@ class ='A'])= 2]

说:


给我所有 a 元素,它们有一个<$ c $在第二个 a 之后的c> class 属性的值等于 B 一个类属性等于 A ,并且只有两个具有 class 属性等于 A


类似地,更一般地,我们可以应用Kayessian方法来找到两个交点节点集。在给出的例子中,我们需要1)在第二个<>之后的同胞集合中的所有 @ class ='B' a class =A/> 和2)第三个< a class =A/> 。这两个集合的交集正好是这两个分隔元素之间的节点,可以这样表示:

  / * / a [@ class ='A'] [2] / following-sibling :: a [@ class ='B'] [
count(。| / * / a [@ class ='A'] [ 3] / preceding-sibling :: a [@ class ='B'])= $ b $ count(/ * / a [@ class ='A'] [3] / preceding-sibling :: a [@class ='B'])]


For the example HTML below, an XPath query that returns the siblings of the "a" elements with class='A' that have class='B' can be written as: //a[@class='A']/following-sibling::a[@class='B']. This query outputs 4 <a class="B"/> elements.

However, I would only like the <a class="B"/> elements that follow the current <a class="A"/> element, and no others that follow other <a class="B"/> elements/nodes. In other words, I only want the following <a class="B"/> sibling elements until the next <a class="B"/> element shows up.

Example HTML:

<a class='A' />
<a class='B' />
<a class='A' />
<a class='B' />
<a class='B' />
<a class='B' />

Any ideas on how to limit my current XPath query to just those siblings would be much appreciated :)

解决方案

To select all a elements having a class attribute of B between some specific a with a class equal to A and the next such occurrence:

/*/a[@class='A'][$n]/following-sibling::a[
    @class='B' and count(preceding-sibling::a[@class='A'])=$n]

This selects everything between the nth a[@class='A'] and the next such element. For a specific example, consider the following input:

<r>
  <a class="A"/>  
  <a class="B"/>  
  <a class="A"/>  
  <a class="B"/>  
  <a class="B"/>  
  <a class="A"/>  
  <a class="B"/>  
  <a class="B"/>  
  <a class="B"/>
</r>

To get the two elements between the second <a class="A"/> and the third <a class="A"/>:

/*/a[@class='A'][2]/following-sibling::a[
    @class='B' and count(preceding-sibling::a[@class='A'])=2]

In English, this says:

Give me all of the a elements having a class attribute whose value is equal to B that come after the second a having a class attribute equal to A and that have only two preceding siblings having a class attribute equal to A

Similarly, and more generally, we can apply the Kayessian method for finding the intersection of two node sets. In the example given, we want the intersection of all the @class='B' elements in 1) the set of siblings after the second <a class="A"/> and 2) the set of siblings before the third <a class="A"/>. The intersection of these two sets is precisely the nodes that come between those two divider elements and can be expressed like this:

/*/a[@class='A'][2]/following-sibling::a[@class='B'][
    count(.|/*/a[@class='A'][3]/preceding-sibling::a[@class='B'])=
    count(/*/a[@class='A'][3]/preceding-sibling::a[@class='B'])]

这篇关于XPath:如何选择以下兄弟姐妹直到某个兄弟姐妹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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