Inserting White Space Using <xsl:text>
The <xsl:text>
element sends literal text to the output stream. Because this element forces any white space information within it to be retained, it can be used to send multiple white space characters as part of a string of text. Each white space character is entered directly by keyboard, or represented as an entity.
How the XSLT processor treats white space in the document depends on the white space it receives from the Microsoft® Document Object Model (DOM) processor. Keep this in mind as you follow the examples in this section.
Example
In this example, the first pair of <xsl:value-of>
elements are separated by four spaces. Because this text node contains white space only, it is stripped from the result tree by the XSLT processor. In the second pair of <xsl:value-of>
elements, however, the four spaces are preserved by the <xsl:text>
element. The following is the HTML generated by these sections of the style sheet.
White space stripped:<-here<br> White space preserved: <-here
Note that four spaces are retained in the second line. However, Internet Explorer, following standard browser behavior, normalizes multiple spaces in an HTML document to a single space on output.
XML File
None. The XSLT file calls itself. View the XSLT file in Internet Explorer.
XSLT File (whitespace1.xsl)
<?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="whitespace1.xsl" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head><title>Test for WhiteSpace1.xsl</title></head> <body> <!-- Put literal text in the result tree: a phrase, four spaces, and the word "Testing." --><xsl:value-of select="'White space stripped:'"/> <xsl:value-of select="'<-here'"/>
<br/> <!-- Put literal text in the result tree: a phrase, four spaces, and the word "Testing." This time, though, enclose the four spaces in an <xsl:text> element. --><xsl:value-of select="' White space preserved:'"/>
<xsl:text> </xsl:text>
<xsl:value-of select="'<-here'"/>
</body> </html> </xsl:template> </xsl:stylesheet>
Formatted Output
White space stripped:<-here
White space preserved: <-here
Processor Output
<html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-16"> <title>Test for WhiteSpace1.xsl</title></head> <body>White space stripped:<-here<br> White space preserved: <-here</body> </html>
See Also
Character and Entity References