I need to add an xmlns to the root element in the output of this XSLT transform. I’ve tried added <xsl:attribute name="xmlns"> but it’s disallowed.
Has anyone got any ideas how I could solve this?
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<xsl:variable name="rootElement" select="name(*)"/>
<xsl:element name="{$rootElement}">
<xsl:apply-templates select="/*/*"/>
</xsl:element>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You can’t simply “add” a namespace, at least not in XSLT 1.0. Namespaces are fixed properties of the input nodes. You copy the node, you copy its namespace as well.
This means you’d have to create new nodes that are in the correct namespace. If you don’t want a prefix, but a default namespace instead, the XSL stylesheet has to be in the same default namespace.
The following applies the default namespace to all element nodes and copies the rest:
turning
to