 <p>Depending on your XSLT processor you might first want to check whether there is support the the XPath 3.0 <code>serialize</code> function <a href="https://www.w3.org/TR/xpath-functions-30/#func-serialize" rel="nofollow noreferrer">https://www.w3.org/TR/xpath-functions-30/#func-serialize</a> or a built-in extension function to do the job.</p> <p>If you want to perform it with XSLT then, in your template, you need to process the attributes as well, e.g. </p> <pre><code>&lt;xsl:template match="*" mode="serialize"&gt; &lt;xsl:text&gt;&amp;lt;&lt;/xsl:text&gt; &lt;xsl:value-of select="name()"/&gt; &lt;xsl:apply-templates select="@*" mode="serialize"/&gt; ... &lt;/xsl:template&gt; </code></pre> <p>Be aware that worked out solutions like <a href="http://lenzconsulting.com/xml-to-string/" rel="nofollow noreferrer">http://lenzconsulting.com/xml-to-string/</a> exist and are probably better then a quick attempt with some templates as a proper serialization which really produces namespace well-formed XML that round-trips is quite a challenge. </p>