如何在模板匹配表达式中使用包含模式的 XSLT 变量
How can I use an XSLT variable containing a pattern in a template match expression
我愿意
<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 版中尝试过,但没有成功。有什么提示吗?
原因:模式有点复杂,我想在几个地方使用它,而不仅仅是这个匹配。
编辑:
我现在通过接受变量不包含 string/pattern 但结果节点这一事实解决了我的问题。如果我修改为
<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,您可以使用带有字符串值的静态参数,然后使用所谓的影子属性 (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>
我建议使用函数而不是变量:
<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>
我愿意
<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 版中尝试过,但没有成功。有什么提示吗?
原因:模式有点复杂,我想在几个地方使用它,而不仅仅是这个匹配。
编辑:
我现在通过接受变量不包含 string/pattern 但结果节点这一事实解决了我的问题。如果我修改为
<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,您可以使用带有字符串值的静态参数,然后使用所谓的影子属性 (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>
我建议使用函数而不是变量:
<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>