Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition to @Tomalak who has provided the precise answer, <strong>do note that <code>&lt;xsl:namespace&gt;</code> is not intended to create a namespace declaration to be used by the XSLT processor generally</strong> with all elements or attributes.</p> <p>The purpose of <code>&lt;xsl:namespace&gt;</code> is to create a specific <em>namespace node</em>. This node has a limited scope only: the current element or attribute, and all children of the current node, if they do not re-assign the prefix to another namespace-uri.</p> <p>Using <code>&lt;xsl:namespace&gt;</code> is necessary only if we want to create a namespace dynamically for a namespace-uri that must be generated dynamically (was not known statically at the start of the transformation). Such cases are extremely rare.</p> <p><strong>In all cases when the desired namspace-uri is known statically, simply declare this namespace at a suitable level of visibility</strong> (usually at the <code>&lt;xsl:stylesheet&gt;</code> instruction) and then simply use the associated prefix, anywhere this namespace must be used.</p> <p><strong>UPDATE:</strong> I have just confirmed in a dialog with specialists in another forum that this is not possible to do with <code>&lt;xsl:namespace&gt;</code>. It adds a namespace node with no name to the current element, but literal result elements are copied 1:1 and remain in their (no) namespace.</p> <p>Here is how Dr. Michael Kay, the Editor of the W3C WG on XSLT explains this:</p> <p>"<em>You need to create elements and attributes with the correct expanded name at the time you create them. If that means using <code>xsl:element</code>, so be it. <code>xsl:namespace</code> can only be used to create additional namespace nodes to those that are created automatically for the prefixes/uris used in element and attribute names; it can't be used to modify the name of an element or attribute node. As always, to understand this you need to understand the data model for namespaces. An element/attribute name is a triple, containing <code>(prefix, uri, localname)</code>. A namespace node is a pair <code>(prefix, uri)</code>. There is a consistency rule that if an element or attribute name exists containing <code>prefix=P</code> <code>uri=U</code> then there must be a namespace node <code>(P, U)</code>. The namespace fixup process ensures that this namespace node is created automatically when you create an element or attribute. <code>xsl:namespace</code> is there to allow you to create additional namespace nodes, typically for namespaces used in QName-valued content</em>".</p> <p>If such result is needed, the solution is to use a second pass and to convert any element belonging to "no namespace" to the desired new namespace.</p> <p><strong>This is the transformation to use in the second pass</strong> (the two passes can be combined into a single stylesheet/transformation):</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:variable name="vUrl" select="'my:Url'"/&gt; &lt;xsl:template match="*[namespace-uri()='']"&gt; &lt;xsl:element name="{name()}" namespace="{$vUrl}"&gt; &lt;xsl:copy-of select="@*"/&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When the above transformation is applied</strong> on the following sample (pass-1-result) xml document:</p> <pre><code>&lt;a&gt; &lt;b&gt; &lt;c/&gt; &lt;/b&gt; &lt;/a&gt; </code></pre> <p><strong>The desired result is produced</strong>:</p> <pre><code>&lt;a xmlns="my:Url"&gt; &lt;b&gt; &lt;c/&gt; &lt;/b&gt; &lt;/a&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload