I am having kind of more trouble with this than I should and have not been able to find a solution that seems to be right:
I want to simply change the namespace of an XML document using xslt 1.0 - used withing a java application, using javax.xml.transform.
This is my xml document:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns="http://namespace1.org" type="Document" version="V2_2">
<Content>
<Text>asdf</Text>
</Content>
</Root>
This is how it should look after transformation:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns="http://namespace2.org" type="Document" version="V2_2">
<Content>
<Text>asdf</Text>
</Content>
</Root>
This is my xslt code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://namespace2.org">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="http://namespace2.org">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
And this is the actual output I get after the transformation:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Root xmlns:ns0="http://namespace2.org">2012-11-02T15:39:46.05+01:00DocumentV2_2<ns1:Content xmlns:ns1="http://namespace2.org">
<ns2:Text xmlns:ns2="http://namespace2.org">asdf</ns2:Text>
</ns1:Content>
</ns0:Root>
There are way to many prefixes added, which I do not want.
I have been able to get rid of the ns+increment prefix by using the following xlst:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://namespace2.org"
xmlns:cmp="http://namespace2.org"
exclude-result-prefixes="cmp">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="cmp:{name()}" namespace="http://namespace2.org">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
which produces the following output:
<?xml version="1.0" encoding="UTF-8"?>
<cmp:Root xmlns:cmp="http://namespace2.org" type="Document" version="V2_2">
<cmp:Content>
<cmp:Text>asdf</cmp:Text>
</cmp:Content>
</cmp:Root>
But I have not been able to get rid of the 'cmp' prefix.
Any ideas how I could achieve the desired output?