至于文本替换,使用 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>