How to copy namespace and attributes of root node with XSLT? <p>I'm new to XSL/XML and I need help with XSL transformation.</p> <p>I have XML which starts like this</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../ws/Invoice.xsd" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"&gt; &lt;ext:UBLExtensions&gt; &lt;ext:UBLExtension&gt; &lt;cbc:ID&gt;INVOICE1&lt;/cbc:ID&gt; &lt;cbc:Name&gt;InvoiceIssuePlaceData&lt;/cbc:Name&gt; &lt;ext:ExtensionAgencyURI&gt;urn:invoice:hr:issueplace&lt;/ext:ExtensionAgencyURI&gt; &lt;ext:ExtensionContent&gt; &lt;ext:InvoiceIssuePlace&gt;London&lt;/ext:InvoiceIssuePlace&gt; &lt;/ext:ExtensionContent&gt; &lt;/ext:UBLExtension&gt; &lt;/ext:UBLExtensions&gt; &lt;cbc:UBLVersionID&gt;2.1&lt;/cbc:UBLVersionID&gt; &lt;cbc:ID&gt;01 1206-2406-568&lt;/cbc:ID&gt; &lt;/Invoice&gt; </code></pre> <p>I want to create elements for all attributes, and to create element between &lt;_> tags for the value of node which has attribute.</p> <p>Here is the xsl...</p> <pre><code>&lt;?xml version='1.0' encoding='utf-8' ?&gt; &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../ws/Invoice.xsd" version="1.0"&gt; &lt;xsl:output method="xml"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*|*|text()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|*|text()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*[@*]"&gt; &lt;xsl:element name="{name()}"&gt; &lt;xsl:if test="count(@*)&gt;=count(node())"&gt; &lt;_&gt; &lt;xsl:value-of select="."/&gt; &lt;/_&gt; &lt;/xsl:if&gt; &lt;xsl:for-each select="@*"&gt; &lt;xsl:element name="{name()}"&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:element&gt; &lt;/xsl:for-each&gt; &lt;xsl:if test="count(node())&gt;count(@*)"&gt; &lt;xsl:apply-templates select="*|text()"/&gt; &lt;/xsl:if&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>The problem is that this transformation doesn't transforms root node (Invoice) as I would need. I'm getting:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"&gt; &lt;xsi:schemaLocation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../ws/Invoice.xsd&lt;/xsi:schemaLocation&gt; </code></pre> <p>No other attributes in Invoice and as new elements. Only xsi:schemaLocation, but with namespace defined on the level of that node.</p> <p>What I'm doing wrong?</p> <p>Thanks.</p>