修改之前的xml file:
使用下列transformation:
-
如果node name已经是以no:开头,则给该节点加上attribute elementFormDefault,其value为"qualified".
-
否则将所有节点的name加上前缀n0:
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()">
<xsl:choose>
<xsl:when test="starts-with(name(),'n0:')">
<xsl:element name="{name()}">
<xsl:attribute name="elementFormDefault">
qualified
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:when>
<xsl:when test="name()=''">
<xsl:value-of select="."/>
<xsl:copy-of select = "@*"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{concat('n0:', name())}">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:transform>
执行transformation之后的xml:
![使用xslt批量修改xml 节点name_分享_02](https://s2.51cto.com/images/blog/202107/06/7fc7dad54fb2800f0de71024feb1b079.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)