XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / Namespaces and Literal Result Elements / Namespace with prefix for Literal Result Elements
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)
The other option requires more typing, but makes the namespace binding visible. Literal Result Elements can also use another namespace with a prefix, provided neither xsl: as the prefix nor the XSLT namespace URI is used. The notation is unambiguous, though more cumbersome:
Code example: kap01/1.6/stylesheet4.xsl.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<xhtml:html>
<xhtml:head>
<xhtml:title>Ergebnisdokument</xhtml:title>
</xhtml:head>
<xhtml:body>
<xhtml:h1>XSLT-Transformation</xhtml:h1>
<xhtml:p><xsl:value-of select="wurzelelement"/></xhtml:p>
</xhtml:body>
</xhtml:html>
</xsl:template>
</xsl:stylesheet>
Note that unprefixed elements would again be in the null namespace, meaning no namespace is bound to the element, because no default namespace is declared this time. An element belonging to the “xhtml” namespace must now have prefix xhtml. However, the result is even less XHTML than before. Notice that the generated meta element is now in no namespace! Very unattractive.
Code example: kap01/1.06/transformation4.html (still not XHTML).
<?xml version="1.0" encoding="UTF-8"?>
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xhtml:head>
<meta http-equiv="Content-Type"content="text/html; charset=utf-8" />
<xhtml:title>Ergebnisdokument</xhtml:title>
</xhtml:head>
<xhtml:body>
<xhtml:h1>XSLT-Transformation (Version c)</xhtml:h1>
<xhtml:p>Das ist ein Testinhalt aus 'dummy.xml'.</xhtml:p>
</xhtml:body>
</xhtml:html>
A brief aside: prefixed namespaces for Literal Result Elements are useful only in an XML context. To generate an XML document containing MathML elements, for example, these can appear as Literal Result Elements with a namespace and prefix in the stylesheet and appear the same way in the result:
Code example: kap01/1.06/stylesheet_math.xsl.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://www.w3.org/1998/Math/MathML">
<xsl:template match="/">
<ergebnis>
<math:math>
<math:msup>
<math:mfenced>
<math:mrow>
<math:mi>a</math:mi>
<math:mo>+</math:mo>
<math:mi>b</math:mi>
</math:mrow>
</math:mfenced>
<math:mn>2</math:mn>
</math:msup>
</math:math>
</ergebnis>
</xsl:template>
</xsl:stylesheet>
Note that root element <ergebnis> of the resulting XML document is not itself in the MathML namespace because it lacks the corresponding prefix.
Code example: kap01/1.06/transformation_math.xml.
<?xml version="1.0" encoding="UTF-8"?>
<ergebnis xmlns:math="http://www.w3.org/1998/Math/MathML">
<math:math>
<math:msup>
<math:mfenced>
<math:mrow>
<math:mi>a</math:mi>
<math:mo>+</math:mo>
<math:mi>b</math:mi>
</math:mrow>
</math:mfenced>
<math:mn>2</math:mn>
</math:msup>
</math:math>
</ergebnis>
The display above has been tidied for presentation. A processor does not itself insert line breaks when generating XML. This will be explained shortly.
Summary of Literal Result Elements
Whenever a template rule is instantiated, Literal Result Elements in its body are copied “as is” into the result document.
Copying Literal Result Elements also copies their namespace. Without a default namespace declaration, unprefixed LRE have no namespace, also called the null namespace. If Literal Result Elements have a default or prefixed namespace, a corresponding declaration is generated in the result document. (Note: If a namespace URI has multiple prefixes in the source, the processor may choose which one to use in the result.)
Text inside Literal Result Elements is copied unchanged. Less obviously, this also includes line breaks between elements and indentation spaces.
Attributes are copied with Literal Result Elements. Their values may be static or determined dynamically when the current template rule runs, using an XPath expression inside an attribute value template.
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