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.

The missing spaces

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.

   

back to top