Declaring Namespaces for XSLT
To declare a namespace, set the xmlns
attribute on an element. For example:
<xsl:stylesheet version="1.0"xmlns:xsl
="http://www.w3.org/1999/XSL/Transform"xmlns
="http://www.w3.org/TR/REC-html40" >
The <stylesheet>
element declares an XSLT style sheet that will reference elements defined in the XSLT 1.0 and HTML namespaces. The XSLT elements will use an xsl
prefix for the names. The HTML elements, on the other hand, will have no prefixes in their names. The namespace declarations apply to the element and all its children.
Declaring a namespace means choosing a namespace prefix, and binding the prefix to the appropriate namespace URI. When declaring namespaces, remember the following:
- The choice of a namespace prefix is unimportant to the XSLT processor, although it should be meaningful for human readers (for documentation purposes). The main constraint is that it must be a valid XML name. A valid XML name begins with a letter or underscore, and has no embedded spaces. Therefore, an XSLT style sheet does not have to use the conventional
xsl:
prefix. It could use something likexslt:
,transform:
, orXSLT_Stylesheet_Prefix:
, instead. - The choice of the URI, however, is critical. For XSLT style sheets, you can associate any prefix with the XSLT namespace URI, http://www.w3.org/1999/XSL/Transform. But any element or attribute whose name begins with that prefix will be treated as an instruction by the XSLT processor. If you change even a single character of this namespace URI, however, the XSLT processor will assume that an element or attribute whose name starts with the given prefix is something other than an XSLT instruction.
Note Not all URIs take the form of a traditional Uniform Resource Locator (URL), such as http://www.mycompany.com. In the case of the MSXML processor, in particular, Microsoft extensions to the XSLT standard, such as the
msxsl:node-set()
function, are identified by a prefix associated with the URI urn:schemas-microsoft-com:xslt. The following namespace declaration is an example.xmlns:msxsl="
urn:schemas-microsoft-com:xslt
"
This URI form is called a Uniform Resource Name (URN). Again, whether the URI takes the form of a locator or a name, the specific URI cannot be changed if you want to take advantage of a feature supported by a particular processor.