<xsl:preserve-space> and <xsl:strip-space> Example (Windows Embedded CE 6.0)
1/6/2010
This is an example of how 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.
strippreserve.xml
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.
<?xml-stylesheet type="text/xsl" href="strippreserve.xsl" ?>
<document>
<block> </block>
<block>
Some text
</block>
<code> </code>
<code>
Some text
</code>
</document>
strippreserve.xsl
The strippreserve.xsl style sheet 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.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- 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>
</xsl:stylesheet>
Results
When the strippreserve.xsl style sheet is applied to the strippreserve.xml document, the output appears in Internet Explorer as follows.
Code blocks:
"Code" #1: [-]
"Code" #2: [NTNSome-textTNTNT]
Normalized blocks:
"Block" #1: []
"Block" #2: [NTNSome-textTNTNT]
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 Controlling White Space with the DOM.
See Also
Reference
position Function
translate Function