preserve-space_1-input.xml
<?xml version="1.0"?>
<products>
<description><strong>Only</strong> <em>Sommer Sales</em>.</description>
<product
id="p1" price="3250" stock="4"/>
<product
id="p2" price="1000" stock="5"/>
<product
id="p3" price="1200" stock="19"/>
<product
id="p4" price="1500" stock="5"/>
<product
id="p5" price="1225" stock="3"/>
</products>
preserve-space_1-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:strip-space
elements="*"/>
<xsl:preserve-space
elements="description"/>
<xsl:output
indent="no"/>
<xsl:template
match="products">
<products>
<xsl:apply-templates/>
</products>
</xsl:template>
<xsl:template
match="description|strong|em">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template
match="product">
<product
no="{position()}" id="{@id}" price="{@price}" stock="{@stock}"/>
</xsl:template>
</xsl:stylesheet>
preserve-space_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<products><description><strong>Only</strong> <em>Sommer Sales</em>.</description><product
no="2" id="p1" price="3250" stock="4"/><product
no="3" id="p2" price="1000" stock="5"/><product
no="4" id="p3" price="1200" stock="19"/><product
no="5" id="p4" price="1500" stock="5"/><product
no="6" id="p5" price="1225" stock="3"/></products>
We need to use <xsl:preserve-space elements="description"/> to preserve the whitespace only text node between <strong>Only</strong> and <em>Sommer Sales</em>. Note that AltovaXML in XMLSpy has not implemented xsl:strip-space and xsl:preserve-space. AltovaXML is always stripping whitespace only text nodes, even when they are important as in this example!
In this example indention of output has been turned off to make it easier to see the whitespace only text nodes in mixed content.
Updated 2009-03-19