XML technologies / XSLT / Introduction to XSLT 2.0 / Getting started with XSLT and XPath / A comparison: expression vs. instruction / Protecting spaces: The instruction xsl:text
(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 1.)
The solution to the missing spaces is simple, but requires a new instruction: xsl:text. The instruction protects its contained string from the XSLT processor and makes it copy the string unchanged into the result document.
Here this applies to the enclosed space:
<xsl:text> </xsl:text>Once the template rule for <anschrift> has been extended appropriately, the result meets expectations:
<!-- Template-Regel für <anschrift>: -->
<xsl:template match="anschrift">
<xsl:value-of select="strasse"/>
<xsl:text> </xsl:text>
<xsl:value-of select="nr"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="plz"/>
<xsl:text> </xsl:text>
<xsl:value-of select="ort"/>
</xsl:template>
The comma and its following space are also enclosed in the instruction. This prevents the next line break from being copied; it lies in the following whitespace node.
The result source now looks more satisfactory:
...
<p>
<b>
Peter
Mustermann
</b><br>
Wagenstr. 5a, 12345 Beispielshausen
</p>
...
When should xsl:text be used?
The instruction xsl:text protects its contained string and instructs the XSLT processor to copy it unchanged into the result document. This is particularly useful for spaces that would otherwise be removed.
In general, xsl:text is used wherever whitespace must enter the result document in a controlled way. Another use of the instruction is discussed in connection with numbering.
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