0

我想要做

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

它工作正常。

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

4

2 回答 2

2

至于文本替换,使用 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>
于 2016-09-07T10:57:30.923 回答
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>
于 2016-09-07T11:11:22.260 回答