XML technologies / XSLT / Introduction to XSLT 2.0 / Getting started with XSLT and XPath / Namespaces and literal result elements / Default namespace for literal result elements

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 1.)

There are two ways to do this. First, you could continue to omit a prefix and declare a default namespace in the stylesheet element, in addition to the XSLT namespace:

Code example: kap01/1.6/stylesheet3.xsl.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:template match="/">
    <html>
      <body>
        <h1 align="center">Dies ist ein Ergebnisdokument.</h1>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

The xmlns pseudo-attribute is used here without a prefix. The declared namespace applies to all unprefixed elements within the stylesheet element. Once a default namespace is declared, all unprefixed elements automatically belong to it.

In this particular case, the default namespace is an XHTML namespace, to which all unprefixed elements are automatically assigned. This information is also passed to the result document as a namespace declaration.

Trying this reveals that the XSLT processor produces an XML document in the XHTML namespace rather than the valid XHTML document you might expect.

Code example: kap01/1.6/transformation3.html (unfortunately only apparently XHTML).

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Ergebnisdokument</title>
  </head>
  <body>
    <h1 align="center">XSLT-Transformation (Version b)</h1>
    <p>Das ist ein Testinhalt aus 'dummy.xml'.</p>
  </body>
</html>

The XML prolog is not a problem, but the mandatory XHTML DOCTYPE declaration. At least the default namespace appears as a declaration on the root element. Clearly, however, supplying a namespace alone is not enough to generate XHTML.

Another interesting detail is that the stylesheet and source encodings, both ISO-8859-1, do not affect the result document’s encoding, which is UTF-8. Also note the generated meta element; it would be useful to control that too.

   

back to top