选择未包含在其他类型元素中的后代元素

时间:2015-08-28 13:23:39

标签: xslt xpath xslt-1.0

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

例如,查找当前元素中包含的所有<a>个链接,这些链接不在<p>中。但是如果当前元素本身位于<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()等。

1 个答案:

答案 0 :(得分:5)

这是一个可能的XPath:

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

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