【问题标题】:XSLT template match: recipe for moving disallowed axes to predicateXSLT 模板匹配:将不允许的轴移动到谓词的方法
【发布时间】:2013-12-13 20:10:18
【问题描述】:

我了解 XSLT 1.0 标准不允许匹配表达式的 StepPatern 部分中的大多数 XPath 轴。 (请参阅this question,其中推荐的替代方法是在Predicate 中使用所需的轴。)

我有一个复杂的 XPath 表达式,它返回一个节点集,node-set-expression。我想做一个匹配 node-set-expression/ following-sibling::* 的模板。有没有一种通用的方法来重写它以使用谓词,以便可以在 XSLT template 元素的 match 属性中使用它?

同样,有没有一种通用的方法来翻译以下内容:

node-set-expression/ preceding-sibling::*

node-set-expression/ self-and-following-sibling::*(这是简写;我知道是not a valid axis

如果谓词不起作用,还有其他通用方法吗?

【问题讨论】:

  • 您能否包含一个示例 XML 文件、您要匹配的节点的模板以及您要定位的节点?

标签: xslt xpath match axes


【解决方案1】:

在 XSLT 2.0 中,我倾向于通过在全局变量中预选匹配节点来处理这种情况:

<xsl:variable name="special-nodes" select="//something/preceding-sibling::*"/>

<xsl:template match="*[. intersect $special-nodes]"/>

在 XSLT 3.0 中,这将进一步简化为

<xsl:template match="$special-nodes"/>

这样做的一个好处是,在执行应用模板时,搜索“特殊节点”一次可能比针对每个此类模式测试每个节点要高效得多;在我看来,这也使情况更加清晰。

对于 XSLT 1.0 的问题,我知道的唯一通用解决方案是将模式编写为

<xsl:template match="*[count(.|//something/preceding-sibling::*) =
                       count(//something/preceding-sibling::*)]">

但这实在是太低效了,无法考虑。

【讨论】:

    猜你喜欢
    • 2015-02-18
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2021-10-18
    • 2014-07-26
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    相关资源
    最近更新 更多