Multiple Namespaces on an element with XSLT 1.0
namespaces, xslt
Solution
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:old="http:\\OldNameSpace.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="old xsi">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pNewNamespace" select="'http:\\NewNameSpace.com'"/>
<xsl:variable name="vXsi" select="document('')/*/namespace::*[name()='xsi']"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="old:*">
<xsl:element name="{local-name()}" namespace="{$pNewNamespace}">
<xsl:copy-of select="$vXsi"/>
<xsl:copy-of select="@*"/>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
when applied on the following XML document:
<Header xmlns="http:\\OldNameSpace.com">
<Detail/>
</Header>
produces (what I guess is) the wanted, correct result:
<Header xmlns="http:\\NewNameSpace.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Detail/>
</Header>
Problem
I am using Microsoft's XSLT processor (1.0 only) XML opening lines: ``` <?xml version="1.0" encoding="utf-8"?> <Header xmlns="http:\\OldNameSpace.com"> <Detail> ``` Have the following XSLT template to pick up the `<Header>` element of my document and change its namespace. ``` <xsl:template match="*"> <xsl:element name="{name()}" xmlns="http:\\NewNameSpace.com"> <xsl:copy-of select="@*"/> <xsl:apply-templates /> </xsl:element> </xsl:template> ``` Which turns `<Header xmlns="http:\\OldNameSpace.com">` Into `<Header xmlns="http:\\NewNameSpace.com">` However I now need to add a second namespace to this so that I get the following output: ``` <Header xmlns="NewNameSpace.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> ``` I have tried using: ``` <xsl:template match="*"> <xsl:element name="{name()}" xmlns="NewNameSpace.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <xsl:copy-of select="@*"/> <xsl:apply-templates /> </xsl:element> </xsl:template> ``` However I still only get the same output as the original XSLT template. Can anyone enlighten to me as to why this is?