In addition to @Tomalak, which provided an accurate answer, please note that <xsl:namespace> not intended to create a namespace declaration that will be used by the XSLT processor as a whole with all elements or attributes.
The goal of <xsl:namespace> is to create a specific node namespace. This node has only a limited scope: the current element or attribute and all children of the current node, unless they redirect the prefix to another uri namespace.
The use of <xsl:namespace> necessary only if we want to dynamically create a namespace for the uri namespace, which should be generated dynamically (it was not statically known at the beginning of the conversion). Such cases are extremely rare.
In all cases where the desired namspace-uri is known statically, simply declare this namespace at an appropriate level of visibility (usually in the <xsl:stylesheet> statement), and then just use the associated prefix, it should be used anywhere in this namespace.
UPDATE: I just confirmed in a dialogue with specialists from another forum that this cannot be done with <xsl:namespace> . It adds an unnamed namespace node for the current element, but the literal elements of the result are copied 1: 1 and remain in their (none) namespace.
Here is how Dr. Michael Kay, WG W3C XSLT Editor explains:
"You need to create elements and attributes with the correct extended name at the time of their creation. If this means using xsl:element , let it be so. xsl:namespace can only be used to create additional namespace nodes for these that are automatically created for prefixes / uris, the attribute names used in the element; it cannot be used to change the element name or node attribute. As always, to understand this, you need to understand the data model for the Namespace. The element / attribute name is a triple containing (prefix, uri, localname) (prefix, uri, localname) . The node namespace is a pair (prefix, uri) . There is consistency in the rule that if there is an element or attribute name containing prefix=P uri=U then there must be a node (P, U) namespace. The process of correcting the namespace ensures that this node namespace is created automatically when the element or attribute is created. xsl:namespace so that you can create additional namespace nodes, usually for namespaces used in QName-value content.
If such a result is necessary, the solution should use the second pass and convert any element belonging "without a namespace" to the desired new namespace.
This conversion is for use in the second pass (two passes can be combined into one stylesheet / transformation):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="vUrl" select="'my:Url'"/> <xsl:template match="*[namespace-uri()='']"> <xsl:element name="{name()}" namespace="{$vUrl}"> <xsl:copy-of select="@*"/> <xsl:apply-templates/> </xsl:element> </xsl:template> </xsl:stylesheet>
When the specified conversion is applied in the following (pass-1-result) xml document example:
<a> <b> <c/> </b> </a>
The required result is obtained :
<a xmlns="my:Url"> <b> <c/> </b> </a>