Select 个未包含在另一种元素类型中的后代元素

Select descendant elements that are not contained in another type of element

在 XPath 1.0 中,我如何 select 当前(上下文)节点 A 的所有后代节点 C,它们不包含在类型 B 的中间节点中?

例如,查找包含在当前元素中但不在 <p> 内的所有 <a> 链接。但如果当前元素本身在 <p> 中,那是无关紧要的。

<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>

例子中的...可以是几个深度的中间节点。

我正在编写 XSLT 1.0,因此可以使用附加功能 generate-id()current() 等。

这是一种可能的 XPath :

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

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