XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / A comparison: expression versus instruction / The problem with whitespace nodes
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 1.)
At first glance, things look fairly good. The source reveals some flaws, however: address formatting is not yet as intended. The XSLT processor produces this result, with spaces shown explicitly:
...
<p>
<b>
Peter
Mustermann
</b><br>
Wagenstr.5a,
12345Beispielshausen
</p>
...
Code example: kap01/1.16.1/adressen.html.
Some details may not matter: browsers remove excess spaces and line breaks when displaying HTML, so these can be ignored for now.
Missing spaces between street and house number, or postcode and town, are more troublesome.
Figure: the missing spaces.
Most line breaks and indentation come from the source formatted for readability. Regions between elements consisting only of spaces and line breaks are called whitespace nodes.
Which characters count as whitespace?
A whitespace node is a text node containing only space (SPC, #20), line feed (LF, #0A), tab (TAB, #09) and carriage return (CR, #0D). In XML 1.1, mainframe newline characters #x85 and #x2028 also count as whitespace. The XML parser already replaces every source line break with LF while reading the document.
Extracting the string value of <name> includes line breaks too. xsl:value-of performs no normalization: leading and trailing spaces remain.
This applies only to source whitespace. Line breaks between xsl:value-of instructions in the address template in the stylesheet are omitted, except where the comma appears.
Stylesheet whitespace nodes do not count as literal character data, which would otherwise be copied. A text node with even one usable character is copied in full.
Explanation
Whitespace nodes between source elements are significant (significant) and are copied to the result tree. Whitespace nodes between stylesheet instructions or Literal Result Elements are discarded.
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