XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / Controlling the result format
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)
Earlier we produced HTML without explicitly requesting it. Fortunately, HTML output does not always require that: the processor can partly infer the intention.
If element <html> is the first literal result element, preceded at most by a whitespace node, in the first processed template rule, the XSLT processor infers HTML output and switches modes.
With any name other than <html>, XML is produced. Correct XHTML output is trickier: providing its namespace alone is insufficient.
Default HTML output
HTML output uses empty tags such as <br> or <hr> rather than XML forms <br/> and <hr/>, following SGML syntax, so the result is not well-formed XML.
A meta tag is also added, giving the encoding, here UTF-8, the processor’s default, unlike the source:
<html>
<head>
<title>Ergebnisdokument</title>
</head>
<body>
<h1 align="center">Die erste XSLT-Transformation</h1>
<p>Ein Textabsatz. Später mehr.</p>
</body>
</html>
What if XHTML is wanted? The processor cannot infer that because XHTML also uses <html> as its root element. We need a way to state the desired output explicitly.
Declaration xsl:output
If ordinary HTML is satisfactory, nothing else is needed. Otherwise a declaration controls many result-document parameters: xsl:output.
As a declaration, xsl:output is top-level, a direct child of xsl:stylesheet, on the same level as template rules. It usually goes first, though position does not affect its operation.
The method attribute of xsl:output
The main attribute is method, selecting the output format from four values: xml, html, text and xhtml.
To produce XHTML, start with:
Code example: kap01/1.07/stylesheet5.xsl.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
...
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xhtml"/>
<xsl:template match="/">
...
</xsl:template>
</xsl:stylesheet>
This gives approximately this output, here with AltovaXML:
Code example: kap01/1.07/transformation5_altova.html (not yet XHTML).
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<title>Ergebnisdokument als XHTML</title>
</head>
<body>
<h1 align="center">Die dritte XSLT-Transformation</h1>
<p>Das ist ein Testinhalt aus 'dummy.xml'.<br/> Es funktioniert.</p>
</body>
</html>
First note the preceding XML declaration, with required attribute version and attribute encoding. To suppress it, set on xsl:output attribute omit-xml-declaration="yes". Empty elements such as <br/> also appear in XML notation.
Saxon, instead of <br/> still outputs <br></br>, which some browsers misread as two breaks. Saxon stays in XML mode until an XHTML namespace is declared.
Unlike HTML, indentation is missing, which is tolerable and fixable below. More annoyingly, umlauts are not replaced by HTML entities and UTF-8 remains the default encoding. Correct this with another attribute of xsl:output.
Complete output declaration for XHTML
Several options are needed. First add to xsl:stylesheet the XHTML namespace as the default namespace. Declaration xsl:output takes attributes encoding, indent, doctype-public and doctype-system:
Code example: kap01/1.07/stylesheet6.xsl.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
...
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
...
</xsl:stylesheet>
The result is now correct XHTML:
Code example: kap01/1.07/transformation6_altova.html (finally XHTML).
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Ergebnisdokument als XHTML</title>
</head>
<body>
<h1 align="center">Die dritte XSLT-Transformation</h1>
<p>Das ist ein Testinhalt aus 'dummy.xml'.<br /> Es funktioniert. Dies ist jetzt XHTML.</p>
</body>
</html>
The namespace
Some recommend putting XHTML namespace "http://www.w3.org/1999/xhtml" on root element <html> within the stylesheet. It is just as good, or better, to declare it by default on xsl:stylesheet itself. This version is therefore excessive and adds no value:
<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 xmlns="http://www.w3.org/1999/xhtml">
...
</html>
</xsl:template>
...
The encoding attribute of xsl:output
To replace default UTF-8, add the encoding attribute:
<xsl:output method="xhtml" encoding="ISO-8859-1"/>Encoding can also be changed for output method="html"; its meta tag then contains, for example, charset=ISO-8859-1:
<xsl:output method="html" encoding="ISO-8859-1"/>Enabling indentation with indent
Restore indentation using the indent attribute, accepting no and yes:
<xsl:output method="xhtml" indent="yes"/>For output method html the explicit default is "yes", as seen in the result. Indentation makes no sense for text output with method="text", where the indent attribute is ignored.
For methods xml and xhtml the specification is silent. Empirically the default seems to be "no", at least in Saxon 9. Other processors may differ, so set it explicitly to be certain.
The result document’s doctype
A doctype can have a PUBLIC identifier naming the declaration, a SYSTEM identifier giving the DTD URI, or both.
Correct XHTML should specify both. These values produce XHTML 1.0 strict:
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
The result is:
Code example: XHTML 1.0 strict doctype.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
For XHTML 1.0 transitional and XHTML 1.0 frameset generate the corresponding doctypes:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
Summary of xsl:output
Further attributes exist for xsl:output, some rarely used or only for specific methods. Full discussion is beyond this introduction; see XSLT 2.0 element reference for complete coverage. Later sections also discuss secondary result documents and other aspects.
Declaration
xsl:outputcontrols result generation. Without it, the processor has different defaults for different document types.Without
xsl:outputthe processor infers the output method from the result’s root.When using
xsl:output, specify not just themethodattribute but also theencodingandindentbehavior, and the doctype for XHTML.
Next consider other content in template rules besides literal result elements: XSLT instructions.
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