Dimitre Novatchev's solution is fine, but I would also like to point out that if you also need to change the namespaces of the nested elements, the following will work better:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="zzz"> <trade ID="{TradeId}"> <xsl:apply-templates select="*[not(self::TradeId)]" mode="change-ns"/> </trade> </xsl:template> <xsl:template match="@*|node()" priority="-10" mode="change-ns"> <xsl:copy/> </xsl:template> <xsl:template match="*" mode="change-ns"> <xsl:element name="{name()}" namespace="my:Trade"> <xsl:apply-templates select="@*|node()" mode="change-ns"/> </xsl:element> </xsl:template> </xsl:stylesheet>
eg. if you have the following input
<trade ID="153"> <x:item xmlns:x="my:Trade" someattr="1"> <x:subitem anotherattr="2">A1</x:subitem> <x:subitem anotherattr="3">A2</x:subitem> </x:item> <x:item xmlns:x="my:Trade">B</x:item> <x:item xmlns:x="my:Trade">C</x:item> </trade>
You'll get
<zzz> <TradeId>153</TradeId> <x:item xmlns:x="x:x" someattr="1"> <x:subitem anotherattr="2">A1</x:subitem> <x:subitem anotherattr="3">A2</x:subitem> </x:item> <x:item xmlns:x="x:x">B</x:item> <x:item xmlns:x="x:x">C</x:item> </zzz>
Attributes are added to demonstrate that they are copied correctly, and a separate mode is used to change the namespace templates so that they do not interfere with another code if you want to use them as part of a larger style sheet.
source share