【问题标题】:How can I use an XSLT variable containing a pattern in a template match expression如何在模板匹配表达式中使用包含模式的 XSLT 变量
【发布时间】:2017-01-14 23:45:46
【问题描述】:

我想做

<xsl:variable name="myPattern" select="node1|node2"/>
<xsl:template match="$myPattern">
    ...
</xsl:template>
<xsl:template  match="/">
    ...
    <xsl:for-each select="distinct-values(//$myPattern/name/text()">
        ...
    </xsl:for-each>
</xsl:template>

我在 XSLT 版本 2.0 和 3.0 上尝试过,但无济于事。有什么提示吗?

原因:模式有点复杂,我想在多个地方使用它,而不仅仅是这个匹配。

编辑:

我现在通过接受变量不包含字符串/模式,但包含结果节点的事实解决了我的问题。如果我将其修改为

<xsl:variable name="myNodes" select="//(node1|node2)"/>
<xsl:template match="$myNodes">
    ...
</xsl:template>
<xsl:template  match="/">
    ...
    <xsl:for-each select="distinct-values($myNodes/name/text()">
        ...
    </xsl:for-each>
</xsl:template>

效果很好。

我仍然想知道为什么不能简单地将字符串存储在变量中并在允许文字字符串的任何地方使用它。

【问题讨论】:

  • XSLT 3.0 允许您使用例如match="$xyz" 并解释“$xyz 匹配变量 $xyz 的值中存在的任何节点”。因此,您需要向我们展示一个最小但完整的 XML、XSLT 示例、您期望的输出、您获得的输出或错误,以及有关使用的 XSLT 处理器的信息,以便我们判断哪里出了问题。显然,我希望您的变量声明看起来像例如&lt;xsl:variable name="//node1 | //node2"/&gt;,除非您要查找的元素确实是根元素。
  • @MartinHonnen:你是对的。我想让其他人的问题简短,以便在寻找答案时节省时间。我认为您的评论甚至比您的答案更好,因为这是我为解决它所做的:&lt;xsl:variable name="myNodes" select="//(node1|node2)"/&gt;.
  • @mdr 你必须使用某种evaluate() 函数来将字符串转换为XPath 表达式(如果这就是你的意思)。
  • @michael.hor257k:我想,这就是我要找的。你能告诉我怎么做吗?
  • @mdr,请参阅我的答案的编辑,如果您想要文本替换使用阴影属性。

标签: xml xslt xslt-2.0 xslt-3.0


【解决方案1】:

至于文本替换,在 XSLT 3.0 中,您可以使用带有字符串值的静态参数,然后使用所谓的影子属性 (https://www.w3.org/TR/xslt-30/#shadow-attributes):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:param name="myPattern" static="yes" as="xs:string" select="'node1|node2'"/>

    <xsl:template _match="{$myPattern}">
        <matched name="{node-name()}">
            <xsl:apply-templates/>
        </matched>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:for-each _select="distinct-values(//{$myPattern}/text())">
                <value>
                    <xsl:value-of select="."/>
                </value>
            </xsl:for-each>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

这改变了

<root>
    <node1>a</node1>
    <node2>1</node2>
    <node1>a</node1>
</root>

进入

<root><value>a</value><value>1</value>
        <matched name="node1">a</matched>
        <matched name="node2">1</matched>
        <matched name="node1">a</matched>
</root>

在 XSLT 3.0 中,您可以将变量或参数引用用于模板的 match 模式,但这不是发生文本替换,而是“$xyz 匹配变量 $ 值中存在的任何节点xyz" (https://www.w3.org/TR/xslt-30/#pattern-examples)。

所以 XSLT 是

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:param name="delete" select="//*[contains-token(@class, 'foo')]"/>

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="$delete"/>

</xsl:stylesheet>

XML 输入是

<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <p class="foobar bar">Paragraph 1.</p>
        <p class="foo bar">Paragraph 2.</p>
        <p class="bar">Paragraph 3.</p>
        <p class="foo">Paragraph 4.</p>
    </body>
</html>

符合 XSLT 3.0 的处理器,如 Saxon 9.7 EE 输出

<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <p class="foobar bar">Paragraph 1.</p>

        <p class="bar">Paragraph 3.</p>

    </body>
</html>

【讨论】:

    【解决方案2】:

    我建议使用函数而不是变量:

    <xsl:function name="_:myPattern" as="xs:boolean">
      <xsl:param name="node" as="node()"/>
      <xsl:sequence select="self::node1() | self::node2()"/>
    </xsl:function>
    
    <xsl:template match="node()[_:myPattern(.)]">
     ...
    </xsl:template>
    

    【讨论】:

    • 这似乎符合我的意图:在使用变量(或更确切地说是函数)的任何地方评估模式。但是:哇!对于偶尔的 XSLT 用户/程序员来说,它不是您可以轻松维护的东西。因此我会坚持使用更简单的(见我的编辑)。但我确实学到了很多。
    • 谢谢。我无法让它工作,不确定它是否是一个错字?我尝试了 exists($node/(self::node1 | self::node2)) 似乎可行。或者,也许 $node/(self::node1 | self::node2) 以 element() ? 作为返回类型。
    • 没有将整个线程分页回我的大脑,我怀疑我打算&lt;xsl:sequence select="$node[self::node1() | self::node2()]"/&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 2017-04-29
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多