如何仅向根元素添加命名空间?
我的XML:
<Envelope>
<from>
<contents />
</from>
</Envelope>
我想要的输出:
<Envelope xmlns:tns="Foo">
<from>
<contents />
</from>
</Envelope>
我只能使用这个“xmlns ='Foo'”,而不是“xmlns:tns = ..”:
<xsl:element name="{local-name()}" namespace="Foo" >
<xsl:copy-of select="attribute::*"/>
<xsl:apply-templates />
</xsl:element>
答案 0 :(得分:2)
这是一个完整的转型:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tns="Foo">
<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="/*">
<xsl:element name="{name()}">
<xsl:copy-of select=
"document('')/*/namespace::*[name()='tns']"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
在提供的XML文档上应用此转换时:
<Envelope>
<from>
<contents />
</from>
</Envelope>
产生了想要的正确结果:
<Envelope xmlns:tns="Foo">
<from>
<contents/>
</from>
</tns:Envelope>