更改root xsl上name属性的值

问题描述 投票:2回答:1

我有这个xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://url..." name="nameOfApp">

<test> asd </test>

</application>

我想重命名根name属性,所以它像这样:

<application xmlns="http://url..." name="newName">

请注意它现在如何newName

这是我的xsl

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:t="http://url...">

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

<xsl:template match="application/@name">
        <xsl:attribute name="name">
            <xsl:value-of select="newName"/>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>

第一部分只是身份变换,但后来我尝试更改name属性值,但它不起作用。

我在SE上尝试了其他来源,但他们都将name属性称为“标签名称”,我有不同的东西。我究竟做错了什么?之后的结果保持不变

xml xslt
1个回答
1
投票

你非常接近。您的匹配器中只有两个小问题。

  • 你忘记了元素t:的命名空间application
  • 由于您要将静态文本作为值插入,因此必须将其引用为'newName'

这是一个固定版本

<xsl:template match="t:application/@name">
    <xsl:attribute name="name">
        <xsl:value-of select="'newName'"/>
    </xsl:attribute>
</xsl:template>
© www.soinside.com 2019 - 2024. All rights reserved.