Share via


Controlling White Space with <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.

Note   The way the white space in the document is treated by the XSL Transformations (XSLT) processor depends on the white space it receives from the Microsoft Document Object Model (DOM) processor. Bear this in mind as you follow the examples in this section.

The following whitespace1.xsl style sheet is self-processing, as indicated by the <?xml-stylesheet?> processing instruction. The result tree consists entirely of content provided by the style sheet.

<?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="'&lt;-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="'&lt;-here'"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

The output appears in Internet Explorer as follows.

White space stripped:<-here
White space preserved: <-here

In this example, the first pair of <xsl:value-of> elements are separated by four simple spaces. This white-space-only text node 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:&lt;-here<br>

White space preserved:    &lt;-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.

See Also

Controlling White Space with XSLT

 Last updated on Saturday, April 10, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.