Namespaces in stylesheets

The need for namespaces

The concept of namespaces is very important to XSLT when a stylesheet contains a mixture of XSLT rules and output document elements. It is possible, for example, to imagine the need to create an XML document that contains an element named 'template'. Without namespaces, it would be very difficult for the XSLT processor to interpret the following markup:

<template match="...">
  <template>
    <apply-templates/>
  </template>
</template>

It is an incidental detail that HTML and XSL formatting elements do not happen to clash with XSLT elements in this way. In other cases, there may be such a conflict.

It is also important for the XSLT processor to be able to distinguish between element sets. A processor that understands XSLT 1.0 is expected to be able to interpret stylesheets that conform to a later version of the XSLT standard, and therefore the processor needs to be able to recognize XSLT elements that were invented later, so as not to treat them inadvertently as output elements.

Prefixes

At the heart of the namespaces scheme is the concept of an element prefix, which is separated from the element name using a colon, ':'. For example, the HTML element 'H3' could become 'html:H3', and the XSLT element 'template' could become 'XSLT:template'. Using this approach, the example above can be made valid, in this case by assigning the prefix 'XSLT' to all XSLT elements, and 'MyDoc' to all target document elements:

<XSLT:template match="...">
  <MyDoc:template>
    <XSLT:apply-templates/>
  <MyDoc:template>
</XSLT:template>

Namespace declarations

If prefix names were assigned by the same authorities that created the respective element sets, the same prefix could accidentally be chosen by both parties, and clashes would then still occur. To avoid this happening, namespaces must therefore be assigned an identifier that is guaranteed to be unique. A good, ready-made candidate for this is the URL standard. A URL provides a unique, globally recognized address for the description or DTD that defines the elements and attributes that a namespace covers. For example, HTML-4 is defined at http://www.w3.org/TR/REC-html40, and XSLT is defined at http://www.w3.org/1999/XSL/Transform.

While unique, these text strings would not be ideal as prefix names. First, they often contain characters that are not allowed in XML element names. Second, they are too long to be manageable. However, they can be mapped to locally defined prefixes that are both short and legal. The namespaces standard provides for this mapping using an attribute that includes the prefix 'xmlns' followed by the assigned prefix name. For example:

xmlns:html="http://www.w3.org/TR/REC-html40"


xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

In this example, the HTML-4 namespace is mapped, locally, to the prefix name 'html', and the XSLT namespace is mapped to 'xsl'. The document author (in this case the stylesheet author) decides what prefixes to use, so that conflicts can always be avoided. Note that for historical but now confusing reasons, the convention is to use 'xsl' for XSLT elements, and 'fo' (Formatting Objects) for XSL elements.

The 'xmlns' attribute can be used on the Stylesheet element:

<xsl:stylesheet
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ...
</xsl:stylesheet>

The XSLT standard contains examples that use namespaces for both the XSLT element set and the output elements:

<xsl:stylesheet
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:fo="http://www.w3.org/1999/XSL/Format" >
  ...
  <xsl:template>
    <fo:block><xsl:apply-templates/></fo:block>
  </xsl:template>
  ...
</xsl:stylesheet>

At first sight, DTD authors may think that these examples include two instances of the 'xmlns' attribute on the Stylesheet element, which would be illegal according to the rules of the XML specification. However, from the DTD perspective these are considered to be distinct attributes. The names 'xmlns:xsl' and 'xmlns:fo', for instance, are not the same. Creation of a DTD to validate stylesheets (to ensure correct usage of the XSLT elements), or to offer guided authoring of stylesheets, involves consideration of this and other issues.

Default namespaces

Using prefixes throughout reduces legibility and is unnecessary. One namespace can be assigned to be the default namespace. When no prefix is present, the element is assumed to belong to this default namespace. The following is therefore valid:

<xsl:stylesheet
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns="http://www.w3.org/1999/XSL/Format" >
  ...
  <xsl:template match="para">
    <block><xsl:apply-templates/></block>
  </xsl:template>
  ...
</xsl:stylesheet>

The alternative approach is to make the XSLT elements the default set:

<stylesheet
						xmlns="http://www.w3.org/1999/XSL/Transform"
            xmlns:fo="http://www.w3.org/1999/XSL/Format">
  ...
  <template match="para">
    <fo:block><apply-templates/></fo:block>
  </template>
  ...
</stylesheet>

Theoretically, this method can even be used with HTML output, as the XSLT processor could be clever enough to recognize the HTML URL, realize from this that it is dealing with HTML, understand that current Web browsers cannot cope with output in the form '<html:HTML>…</html:HTML>', and therefore remove the prefixes on output. But some XSLT processors may not do this, and so it is worth testing first.

Attributes that have no prefix are assumed to belong to the same namespace as the elements they belong to, so that they have their own concept of 'default' namespace. A prefix is always needed on an attribute if it belongs to a namespace other than the one the element belongs to. Some XSLT attributes may be used in result elements. For example, the attribute 'use-attribute-sets' may be used in output elements to attach predefined attribute names and values to the element, but when used here they must include the XSLT namespace prefix:

<P xsl:use-attribute-set="common-set" version="1.3">
   ...
</P>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset