XML technologies / XSLT / Introduction to XSLT 2.0 / Getting started with XSLT and XPath / Structure controls processing / Preserving whitespace: xsl:preserve-space
(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 1.)
The declaration xsl:strip-space has a counterpart that can prevent the removal of whitespace nodes, in the form of the declaration xsl:preserve-space. It is needed only if xsl:strip-space has also been used. Preserving whitespace nodes is the processor’s default behavior.
Potential conflicts are also handled: if xsl:strip-space orders whitespace removal for an element and xsl:preserve-space revokes it for the same element, xsl:preserve-space takes precedence, preserving the whitespace. Especially when stripping is global, the declaration can select the elements whose whitespace should remain. In this example, these are <name> and <anschrift>:
<!-- globales Whitespace-Stripping: -->
<xsl:strip-space elements="*"/>
<!-- Widerruf für zwei Elemente: -->
<xsl:preserve-space elements="name anschrift"/>
The order of the two declarations does not matter. They can be swapped without affecting the result.
The result now looks less tidy in the source, but tidy again when displayed in the browser:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>XSLT 2.0 und XPath 2.0 - Adressenliste</title>
</head>
<body>
<h1>Adressenliste</h1>
<p>Name: <b>
Peter
Mustermann
</b><br />Anschrift:
12345
Beispielshausen
Wagenstr.
5a
</p>
...
</html>
Code example: kap01/1.17.10/adressen.html.
In this case, if you really want cleaner source output, revise the template rules. However, since the document is primarily intended for display in a browser, this is not particularly important.
Points to remember about xsl:strip-space and xsl:preserve-space
xsl:strip-spaceprevents whitespace nodes from being copied for the elements named in the declaration.Whitespace stripping applies to the entire content of those elements, including child elements not explicitly listed.
xsl:preserve-spacerevokes whitespace stripping directly or indirectly caused byxsl:strip-spacefor the specified elements.
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 terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn