根据属性值使用 XSLT 更改 XML 标记中的值

Change value in XML tag using XSLT according to the attribute value

我是 XSL 新手,遇到了一个问题。

我有一个 xml 格式如下:

<Destinations>
    <conf:Destination id="12">
        <conf:attributes>
            <conf:attribute1>1212</conf:attribute1>
        </conf:attributes>
    </conf:Destination>
    <conf:Destination id="31">
        <conf:attributes>
            <conf:attribute1>3131</conf:attribute1>
        </conf:attributes>
    </conf:Destination>
</Destinations>

然后说,我有一个带有以下 2 个参数的 xsl:

<xsl:param name="attribute12" select="'21'" />
<xsl:param name="attribute31" select="'5'" />

我想在 XSLT 1 中有一个 xsl 模板来更改我的 xml,如下所示: 1) For destination id=12 in xml, value inside 'conf:attribute1' 标签设置为21 2) 对于 xml 中的目标 id=31,'conf:attribute1' 标签内的值设置为 5

这样我将得到最终的 xml 作为:

<Destinations>
    <conf:Destination id="12">
        <conf:attributes>
            <conf:attribute1>21</conf:attribute1>
        </conf:attributes>
    </conf:Destination>
    <conf:Destination id="31">
        <conf:attributes>
            <conf:attribute1>5</conf:attribute1>
        </conf:attributes>
    </conf:Destination>
</Destinations>

谁能帮忙。

使用身份转换模板

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

然后是两个模板

<xsl:template match="conf:Destination[@id='12']/conf:attributes/conf:attribute1">
  <xsl:copy>
    <xsl:value-of select="$attribute12"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="conf:Destination[@id='31']/conf:attributes/conf:attribute1">
  <xsl:copy>
    <xsl:value-of select="$attribute31"/>
  </xsl:copy>
</xsl:template>