如何在模板匹配表达式中使用包含模式的 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 版尝试过这个,但无济于事.有什么提示吗?
I tried this with XSLT version 2.0 and 3.0 to no avail. Any hints?
原因:模式有点复杂,我想在几个地方使用它,而不仅仅是这个匹配.
Reason: The pattern is a little more complicated and I would like to use it in several places and not just this match.
我现在通过接受变量不包含字符串/模式,但包含结果节点的事实来解决我的问题.如果我修改为
I solved my problem for now by accepting the fact that the variable does not contain the string/pattern, but the result nodes. If I modify it to
<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):
As for textual replacement, with XSLT 3.0 you can use use a static parameter with a string value and then so called shadow attributes (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).
In XSLT 3.0 you can use a variable or parameter reference for the match
pattern of a template but it is not a textual replacement that happens, rather "$xyz matches any node that is present in the value of the variable $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 输入是
and the XML input being
<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 输出
a conforming XSLT 3.0 processor like Saxon 9.7 EE outputs
<html>
<head>
<title>test</title>
</head>
<body>
<p class="foobar bar">Paragraph 1.</p>
<p class="bar">Paragraph 3.</p>
</body>
</html>
这篇关于如何在模板匹配表达式中使用包含模式的 XSLT 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!