XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / preserve-space
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
The declaration xsl:preserve-space specifies the elements within which whitespace nodes should be preserved and copied to the result document. In this respect it is the counterpart of xsl:strip-space.
Classification | Declaration |
Function group | Control whitespace |
Introduced | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:preserve-space is a declaration that may appear only as an immediate child of root element xsl:stylesheet or xsl:transform. The element is always empty.
Attributes:
Standard attributes apply. In addition, xsl:preserve-space has a required attribute elements.
elements
Value | tokens |
Usage | required |
Introduced | XSLT 1.0 |
The elements attribute lists, as space-separated tokens, the names of elements whose whitespace nodes should be preserved. Wildcards such as “*” or “mein-ns:*” may also be used alongside or instead of names.
Purpose:
The instruction xsl:preserve-space preserves whitespace nodes in the contents of elements listed in its token list. Whitespace nodes are text nodes consisting only of spaces, line feeds, carriage returns or tabs.
Whitespace nodes can therefore occur only between elements, not adjacent to text nodes, which would absorb them. Whitespace at the beginning or end of text nodes is significant and is handled by neither xsl:strip-space nor xsl:preserve-space. At most, it can be normalized.
Preserving whitespace nodes is the XSLT processor’s default output behavior. Using xsl:preserve-space is therefore necessary only if the same stylesheet also uses declaration xsl:strip-space and whitespace nodes must always be preserved for certain elements.
Example – Using xsl:preserve-space and xsl:strip-space:
Source document, excerpt:
<beispiel>Dies ist ein <strong>einfaches</strong> <emph>Beispiel</emph> für gemischten Inhalt.</beispiel>
Stylesheet:
...
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="beispiel"/>
...
<xsl:template match="beispiel">
<p><xsl:apply-templates/></p>
</xsl:template>
Result:
Without xsl:preserve-space, within element <beispiel>, even the significant whitespace between end tag </strong> and start tag <emph> would be removed. This is because the elements attribute of xsl:strip-space uses wildcard selector * to cover all elements, including <beispiel>.
Without xsl:preserve-space, the result would look like this, with the problem area highlighted in bold:
<p>Dies ist ein einfachesBeispiel für gemischten Inhalt.</p>
Overriding this with xsl:preserve-space produces the desired output:
<p>Dies ist ein einfaches Beispiel für gemischten Inhalt.</p>
Element definition:
XSLT 1.0:
<!-- Category: top-level-element -->
<xsl:preserve-space
elements = tokens />
XSLT 2.0:
<!-- Category: declaration -->
<xsl:preserve-space
elements = tokens />
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