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

时间:2015-01-10 13:05:52

标签: xml xslt xslt-1.0 xalan

我是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>

并说,我有一个xsl跟随2个参数:

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

我希望在XSLT 1中有一个xsl模板,它可以更改我的xml,如下所示: 1)对于xml中的目标id = 12,将'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>

任何人都可以帮忙。

1 个答案:

答案 0 :(得分:1)

使用身份转换模板

<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>
相关问题