Xpath 仅选择具有匹配属性的直接兄弟 [英] Xpath to select only direct siblings with matching attributes

查看:21
本文介绍了Xpath 仅选择具有匹配属性的直接兄弟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例文档:

<root>
  <p class="b">A</p>
  <p class="b">B</p>
  <p class="a">C</p>
  <p class="a">D</p>
  <p class="b">E</p>
  <x>
    <p class="b">F</p>
  </x>
</root>

我正在寻找一个 xpath 表达式,它选择具有匹配类属性的给定节点的所有 直接 兄弟节点,而不是任何兄弟节点.在上面的例子中,前两个

A-B 应该被选中;同样是两张 <p class="a"> CD,同样是第五张 <p class="b"> E,因为它没有 直接兄弟姐妹;同样, 中的单个

F 也是如此.请注意,在这种情况下,B 和 C 不是直接兄弟姐妹,因为它们具有不同的类属性值!

I am looking for an xpath expression which selects all direct siblings of a given node with matching class attributes, not any sibling. In above example, the first two <p class="b"> A-B should be selected; likewise the two <p class="a"> C-D, likewise the fifth single <p class="b"> E as it has no direct siblings; likewise the single <p class="b"> F inside of <x>. Note that in this context B and C are not direct siblings because they have different class attribute valued!

我拥有的是这个:

xml.xpath("//p") # This selects all six <p> elements.
xml.xpath("//p[@class='b']") # This selects all four <p class="b"> elements.
xml.xpath("//p/following-sibling::p[@class='b']") # This selects all <p class="b"> sibling elements, even though not direct siblings.

最后一个表达式也选择第五个兄弟姐妹,尽管中间有不匹配的兄弟姐妹.

The last expression selects the fifth sibling as well, although there are non-matching siblings inbetween.

如何只选择具有相同 class 值的直系兄弟姐妹?

How do I select only direct siblings with the same class value?

编辑澄清:注意最后两个是单独的选择,而不是兄弟姐妹!

Edit To clarify: note how the last two are individual selections, not siblings!

编辑我在此处保存了一个示例.基于/root/p[1]的Xpath表达式应该选择A、B、C、D.

Edit I have saved an example here. The Xpath expression based on /root/p[1] is supposed to select A, B, C, D.

推荐答案

要获得下一个兄弟姐妹,您可以添加位置 - 1 表示就在旁边.

To get the very next sibling, you can add the position - 1 meaning right beside.

following-sibling::*[1]

为了确保下一个兄弟节点是特定的节点类型,你可以添加以下过滤器,其中p是我们要匹配的节点类型.

To ensure that the next sibling is of a specific node type, you can add the following filter, where p is the node type we want to match.

[self::p]

如果您只想要具有相同属性的那些,您还需要在第一个 p 元素上指定该属性.

If you only want ones with the same attribute, you would also need to specify the attribute on the first p element.

因此,如果您只想要紧跟在 class b p 元素之后的 class b p 元素,则可以执行以下操作.这只会给你第二个 p 元素.

So if you just want class b p elements that are immediately after a class b p element, you can do the following. This would just give you the second p element.

//p[@class='b']/following-sibling::*[1][@class='b'][self:p]

听起来您实际上可能想要任何与另一个 b 类元素相邻的 b 类元素.在这种情况下,您可以检查以下和前面的兄弟.以下将为您提供前 2 个 p 元素.

It sounds like you might actually want any class b element which is adjacent to another class b element. In that case, you can check the following and preceding sibling. The following would give you the first 2 p elements.

//p[@class='b'][following-sibling::*[1][@class='b'][self::p] 
                or preceding-sibling::*[1][@class='b'][self::p]]    

这篇关于Xpath 仅选择具有匹配属性的直接兄弟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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