Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

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.

Example

The strippreserve.xml document below contains <code> and <block> elements that have white space in the form of tabs, spaces, and newline characters.

The strippreserve.xsl style sheet below does the following:

The results are as follows:

Note   This example uses only XSLT to strip and preserve white space. You can also strip or preserve 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 each tabhere and replace it with a tab character (that is, press the TAB key).

<?xml-stylesheet type="text/xsl" href="strippreserve.xsl" ?>
<document>
<block> </block>
<block>
tabhere
Some texttabhere
tabhere
tabhere</block>
<code> </code>
<code>
tabhere
Some texttabhere
tabhere
tabhere</code>
</document>

XSLT File (strippreserve.xsl)

<?xml version='1.0'?>
<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(.,' &#10;&#13;&#9;','-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(.,' &#10;&#13;&#9;','-NRT')"/>]<br/>
</xsl:for-each>
</pre>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Formatted Output

Code blocks:
"Code" #1: [-]
"Code" #2: [NTNSome-textTNTNT]
Normalized blocks:
"Block" #1: []
"Block" #2: [NTNSome-texttabhereNTNT]

Processor Output

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-16">
<title>Test: Stripping/Preserving White Space</title></head>
<body>
<h4>Code blocks:</h4>
<pre>
"Code" #1: [-]<br>
"Code" #2: [NTNSome-textTNTNT]<br></pre>
<h4>Normalized blocks:</h4>
<pre>
"Block" #1: []<br>
"Block" #2: [NTNSome-textTNTNT]<br></pre>
</body>
</html>