选择不包含在其他类型元素中的后代元素 [英] Select descendant elements that are not contained in another type of element

查看:31
本文介绍了选择不包含在其他类型元素中的后代元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XPath 1.0 中,如何选择当前(上下文)节点 A 的所有后代节点 C,这些子节点不包含在类型 B 的中间节点中?

例如,查找当前元素中包含的所有 链接,这些链接不在

内.但是如果当前元素本身在

中,那就无关紧要了.

<——这无关紧要,因为它在当前元素之外...<div><—— 当前元素(上下文节点)...<a></a><—— xpath 应该选择这个节点...<p>...<a></a><—— 但不是这个,因为它在一个 p 中,它在上下文中...<p>...

...</p>

示例中的 ... 可以是多个深度的中间节点.

我正在编写 XSLT 1.0,因此可以使用附加函数 generate-id()current() 等.

这是一种可能的 XPath :

.//a[not(ancestor::p/​​ancestor::* = current())]

此 XPath 检查当前后代 a 元素是否没有祖先 p,而祖先是当前上下文节点.换句话说,它检查 a 元素是否没有祖先 p 介入 a 和当前上下文节点之间.

In XPath 1.0, how can I select all descendant nodes C of current (context) node A, that are not contained in an intervening node of type B?

For example, find all <a> links contained in the current element, that are not inside a <p>. But if the current element is itself inside a <p>, that's irrelevant.

<p>                   <—— this is irrelevant, because it's outside the current element
    ...
    <div>             <—— current element (context node)
        ...
        <a></a>       <—— the xpath should select this node
        ...
        <p>
            ...
            <a></a>   <—— but not this, because it's inside a p, which is inside context
            ...
        <p>
        ...
    </div>
    ...
</p>

The ... in the example could be several depths of intervening nodes.

I'm writing XSLT 1.0, so the additional functions generate-id(), current(), and such are available.

解决方案

This is one possible XPath :

.//a[not(ancestor::p/ancestor::* = current())]

This XPath checks if current descendant a element doesn't have ancestor p which ancestor is current context node. In other words, it checks if the a element doesn't have ancestor p intervening between the a and the current context node.

这篇关于选择不包含在其他类型元素中的后代元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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