XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / namespace
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
The instruction xsl:namespace creates a namespace node in the result sequence.
Classification | Instruction |
Function group | Creating result document output |
Introduction | XSLT 2.0 |
Position in the stylesheet and permitted contents:
The instruction xsl:namespace can occur anywhere inside template rules. It contains a sequence constructor producing the namespace URI string from any XSLT instructions or text literals yielding a string. Alternatively, use the select attribute, which is required when the instruction is empty.
Attributes:
Alongside standard attributes, the instruction xsl:namespace has a required attribute name and a usually optional select attribute.
name
Value | {ncname} (also as an AVT) |
Usage | Required |
Introduction | XSLT 2.0 |
The name attribute names the namespace node, in other words the prefix to use in the result declaration.
An attribute value template can supply the value. It must be an NCName, containing no colon, or the empty string. An empty value gives the namespace URI no prefix, meaning the default namespace. Only “xmlns” is forbidden.
Reserved string “xml” is allowed (Note: unlike earlier working drafts). The namespace URI must then be "http://www.w3.org/XML/1998/namespace"; otherwise static error ERR XTDE0925 is reported. Conversely, assigning that URI when the prefix from attribute name is not “xml” causes the same error.
Note: If the prefix is already bound to another URI on that node, the processor may replace it with an arbitrary generated prefix. This is not an error.
select
Value | xpath-expression |
Usage | Optional ( required for an empty instruction) |
Introduction | XSLT 2.0 |
The select attribute supplies the namespace URI instead of an internal sequence constructor. If select is present, xsl:namespace must be empty, except for xsl:fallback instructions. Conversely, an empty instruction requires select; violations are static errors (ERR XTSE0910).
Purpose:
Namespace nodes normally arise automatically when creating or copying an element or attribute in a declaration’s scope (scope).
The instruction xsl:namespace explicitly creates namespace nodes. Rarely needed, it nevertheless fills a gap in XSLT 1.0 node constructors; example 2 shows an XSLT 1.0 alternative.
Use the instruction xsl:namespace when the URI or prefix is known only at runtime and the namespace is absent from source and stylesheet.
The xsl:namespace instruction binds a prefix to a URI so it cannot be reassigned when allocating prefixes to result namespaces.
Otherwise, the XSLT processor may, during namespace fixup at serialisation, choose any available prefix for a namespace URI. This occurs only once the result tree is complete and every element and attribute has its in-scope namespace nodes.
Fixed prefixes must remain consistent. Such bindings result from instruction xsl:namespace or from applying xsl:copy or xsl:copy-of to namespace nodes. Other URIs must then receive different prefixes.
A URI produced by the sequence constructor or attribute select must not be empty; otherwise an error occurs (ERR XTDE0930). This instruction therefore cannot perform namespace undeclaration (namespace undeclaration); use global attribute undeclare-prefixes="yes" on instruction xsl:output, which serialises the result.
Examples:
Example 1 – Creating a namespace node:
<xsl:namespace name="xsd">http://www.w3.org/2001/XMLSchema</xsl:namespace>
creates this namespace declaration in the result:
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Example 2 – Creating a namespace node in XSLT 1.0:
XSLT 1.0 lacks instruction xsl:namespace. If a namespace must be created, or an older processor might process a stylesheet with xsl:namespace, force its creation as follows:
<!-- (Stylesheet 2.0, also Vorwärtskompatibler Modus) -->
<xsl:template name="machNamensraum">
<!-- Aufruf vor Erzeugung eventueller Kindknoten! -->
<xsl:namespace name="mein" select="'http://www.beispiel.com/namensraum'">
<!-- XSLT 1.0-Prozessor führt Fallback aus: -->
<xsl:fallback>
<xsl:attribute name="mein:egal" namespace="'http://www.beispiel.com/namensraum'">bla bla bla</xsl:attribute>
</xsl:fallback>
</xsl:namespace>
</xsl:template>
In forwards-compatible mode, an XSLT 1.0 processor executes the ignored instruction’s fallback block for instruction xsl:namespace. Without a fallback block, forwards-compatible mode reports an error. This creates an attribute with the desired namespace. Ignore its name and value; the accompanying declaration also creates the namespace node, unless already in scope, in which case only the attribute is created.
Element definition:
XSLT 1.0:
Element unavailable in XSLT 1.0.
XSLT 2.0:
<!-- Category: instruction -->
<xsl:namespace
name = { ncname }
select? = expression >
<!-- Content: sequence-constructor -->
</xsl:namespace>
Cross-references:
Constructors for the other node kinds are xsl:attribute, xsl:element, xsl:comment, xsl:processing-instruction and xsl:text.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn