【问题标题】:Change value in XML tag using XSLT according to the attribute value根据属性值使用 XSLT 更改 XML 标记中的值
【发布时间】:2015-01-10 13:05:52
【问题描述】:

我是 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) 对于 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>

有人可以帮忙吗。

【问题讨论】:

  • 为什么在询问 XSLT 1 时问题被标记为 xslt-2.0
  • 谢谢,已更正。对此有任何帮助..

标签: xml xslt xslt-1.0 xalan


【解决方案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>

【讨论】:

  • 谢谢,你太棒了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 2012-08-03
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多