XSLT - 将每个 XML 属性值匹配并替换为特定属性值

XSLT - Match and replace every XML attribute value with specific attribute value

我想替换来自第三方软件的控件 XML 文件中的通配符。

不幸的是,这些通配符也用作此 XML 文件中的属性值。

我举个例子:

<control>
  <some-tag id="$wildcard1$" version="3.14">
    <another-tag id="second_level">Whosebug rocks!</another-tag>
  </some-tag>
  <some-tag id="foo" version="$wildcard2$"/>
  <some-tag id="bar" version="145.31.1"/>
</control>

我尝试编写一个带有参数的通用转换来替换属性值中的通配符。

我最大的问题是,我不知道属性名称。所以我需要匹配 XML 文件中的每个属性。这很容易 但我如何将每个属性与特定值(例如 $wildcard$)匹配?

这个问题的答案比我想象的要容易得多。

<xsl:template match="@*[. = $wildcard]">
    <xsl:attribute name="{name(.)}">
        <xsl:value-of select="$wildcard_value"/>
    </xsl:attribute>
</xsl:template>

希望对大家有所帮助。

P.S:这是我用于替换属性值中的通配符的完整 XSL 转换:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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:param name="wildcard" required="yes" />
    <xsl:param name="wildcard_value" required="yes" />
    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes" />
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*[. = $wildcard]">
        <xsl:attribute name="{name(.)}">
            <xsl:value-of select="$wildcard_value" />
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>