我有这个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属性称为“标签名称”,我有不同的东西。我究竟做错了什么?之后的结果保持不变
你非常接近。您的匹配器中只有两个小问题。
t:
的命名空间application
'newName'
这是一个固定版本
<xsl:template match="t:application/@name">
<xsl:attribute name="name">
<xsl:value-of select="'newName'"/>
</xsl:attribute>
</xsl:template>