Example of <xsl:preserve-space> and <xsl:strip-space>
You can use the <xsl:preserve-space> and <xsl:strip-space> elements to preserve and strip white space from XML documents. The following strippreserve.xml document contains <code> and <block> elements that have white space in the form of tabs, spaces, and newline characters. The strippreserve.xsl style sheet is applied to the strippreserve.xml document to retain white space within all <code> elements and to strip it from all the block elements.
In the strippreserve.xml document, the first <code> element and first <block> element contain exactly the same amount and kind of white space (a single space), as do the second <code> and <block> elements. However, the second <code> and second <block> elements also contain the phrase "Some text" among the white space. For the strippreserve.xml document, the <?xml-stylesheet type="text/xsl" href="strippreserve.xsl" ?> instruction is included to apply the strippreserve.xsl style sheet to the strippreserve.xml document. When applied, the strippreserve.xsl style sheet causes the <code> elements to retain white space-only content and the <block> elements to lose white space-only content.
Example
The strippreserve.xsl style sheet below does the following.
Preserves the extraneous white space in the <code> elements.
Strips the extraneous white space from the <block> elements.
Encloses the content of each <code> and <block> element in square brackets, [ ].
Provides a numbered label for each <code> or <block> using the XSL Transformations (XSLT) position() function.
Using the XML Path Language (XPath) translate() function, converts "invisible" white space to a visible character, one character at a time. This enables you to "see" the specific white characters in the <code> and <block> elements.
Each space character appears in the result tree as a hyphen (-).
Each newline character appears in the result tree as the letter "N."
Each carriage return appears in the result tree as the letter "R."
Each tab appears in the result tree as the letter "T."
The first <code> element contains a single space, denoted by the hyphen in the [ ] characters. The first <block> element also contains a single space in the source document. However, the <xsl:strip-space> setting has caused this space to be removed from the result tree.
In the second <code> and <block> elements, no white space has been removed. If an element has any non-white space content, white space will not be stripped even if <xsl:strip-space> is set for that element.
Note This represents a "pure XSLT" approach to the stripping and retention of white space. You can also strip or retain white space by manipulating the Document Object Model (DOM). For more information about this approach, see White Space and the DOM.
XML File (strippreserve.xml)
Copy this text to a file, then find and replace each "tabhere" by a tab (press the Tab key).
<!-- Retain white space within all <code> elements -->
<xsl:preserve-space elements="code"/>
<!-- ... but strip it from all <block> elements -->
<xsl:strip-space elements="block"/>
<xsl:template match="/">
<html>
<head><title>Test: Stripping/Preserving White Space</title></head>
<body>
<h4>Code blocks:</h4>
<!-- <pre> element forces all output characters to be same width -->
<pre>
<!-- Use translate() XPath function to convert white-space
characters to "visible" form. -->
<xsl:for-each select="//code">
"Code" #<xsl:value-of select="position()"/>: [<xsl:value-of select="translate(.,'
','-NRT')"/>]<br/>
</xsl:for-each>
</pre>
<h4>Normalized blocks:</h4>
<pre>
<xsl:for-each select="//block">
"Block" #<xsl:value-of select="position()"/>: [<xsl:value-of select="translate(.,'
','-NRT')"/>]<br/>
</xsl:for-each>
</pre>
</body>
</html>
</xsl:template>
Preserving or Stripping White Space
XSL Transformations (XSLT) can distinguish nodes that contain white space intermingled with other characters. The white space is considered inseparable from the other text in the node.
For nodes that contain nothing but white space, the <xsl:preserve-space> and <xsl:strip-space> elements handle how the nodes are output.
Preserving White Space with <xsl:preserve-space>
The <xsl:preserve-space> element provides a list of those elements in the source document where white space must be preserved on output. <xsl:preserve-space> is always an empty, top-level element, for example, a child of the <xsl:stylesheet> element in an XSLT style sheet.
The following is the general syntax.
<xsl:preserve-space elements="elem1 elem2..." />
elem1 and elem2 are the names of all elements (without the enclosing < and > delimiters) whose white space must be preserved.
You can specify all of the elements in the source document with the asterisk operator ("*").
<xsl:preserve-space elements="*" />
Because all of the content of an XML document is, by default, preserved, <xsl:preserve-space> is useful only in cases in which you have used <xsl:strip-space> to override the default behavior; in such cases, use <xsl:preserve-space> to identify exceptions to the explicit stripping of white space.
Note The Microsoft® XSLT processor will process all of a document's white space only if the preserveWhiteSpace property has been set to True prior to loading the document into the DOM. For more information, see How the MSXML Processor Parses White Space.
Removing White Space with <xsl:strip-space>
The <xsl:strip-space> element provides a list of those elements in the source document in which content must be removed from the output tree. <xsl:strip-space> is an empty, top-level element.
The following is the general syntax.
<xsl:strip-space elements="elem1 elem2..." />
elem1 and elem2 are the names of all elements (without the enclosing < and > delimiters) whose white space must be removed.
You can specify all of the elements in the source document with the asterisk operator ("*").
<xsl:strip-space elements="*" />
If an element appears in both an <xsl:strip-space> and <xsl:preserve-space> list, the last specification applies. Therefore, a typical sequence of these two elements in an XSLT style sheet is as follows.
<xsl:strip-space elements="*" />
<xsl:preserve-space elements="elem1 elem2..." />
If this order were reversed, the explicit "preserve" settings for elem1, elem2, and so on would be overridden by the global "strip" setting.