在同一个 xpath 查询中结合使用前后同级 [英] Combining the use of preceding and following sibling in the same xpath query

查看:18
本文介绍了在同一个 xpath 查询中结合使用前后同级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,但我似乎无法解决它.假设我有以下代码:

I have a quite simple problem but i can't seem to resolve it. Let's say i have the following code:

<a>
    <b property="p1">zyx</b>
    <b>wvu</b>
    <b>tsr</b>
    <b property="p2">qpo</b>
    <b>qcs</b>
</a>

我想选择具有 property="p1"b 节点和具有 b 节点之间的节点>property="p2".我可以使用前面的兄弟和后续的兄弟轴来做任何一个,但我似乎无法找到如何将两者结合起来.

I want to select the nodes between the b node who has a property="p1" and the b node who has property="p2". I can do either of those with the preceding-sibling and the following-sibling axis but I can't seem to find how to combine both.

推荐答案

XPath 1.0:

/a/b[preceding-sibling::b/@property='p1' and following-sibling::b/@property='p2']

XPath 2.0:
上面的表达式在 XSLT 2.0 中有一些怪癖,最好使用新的更安全的运算符 <<(之前)和 >>(之后).

XPath 2.0:
The expression above has some quirks in XSLT 2.0, it is better to use the new and safer operators << (before) and >> (after).

/a/b[../b[@property='p2'] << . and . >> ../b[@property='p1']]

这篇关于在同一个 xpath 查询中结合使用前后同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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