XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / strip-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 xsl:strip-space declaration specifies the elements within which whitespace nodes are removed so they are not copied to the result document.
Classification | Declaration |
Function group | Control whitespace |
Introduced | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:strip-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:strip-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 contents should have whitespace nodes removed. Wildcards such as “*” or “mein-ns:*” may also be used alongside or instead of explicit names.
Purpose:
The instruction xsl:strip-space removes whitespace nodes from 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.
An XSLT processor preserves whitespace nodes in its output by default. xsl:strip-space can remove them selectively for certain elements or globally for all elements.
For example, elements="*" removes whitespace nodes from all elements regardless of their names. This global declaration can be overridden for specific elements using xsl:preserve-space, as shown in the example.
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, the significant whitespace between elements within elements and <beispiel> would also be removed because xsl:strip-space applies globally to all elements with its wildcard selector elements="*".
The output would look like this, highlighted:
<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:strip-space
elements = tokens
/>
XSLT 2.0:
<!-- Category: top-level-element -->
<xsl:strip-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