[xsl] Preserve-space isn't

Subject: [xsl] Preserve-space isn't
From: "Trevor Nicholls" <trevor@xxxxxxxxxxxxxxxxxx>
Date: Fri, 31 Mar 2006 17:31:46 +1200
Hi

I am using XML to hold documents. For the most part I don't care about
whitespace, but I have certain elements which are space-sensitive, e.g. a
syntax statement.

I have a DTD defining the documents (mydocs.dtd); included in this DTD are
the following lines:
========== DTD ==========
<!ENTITY % simple "#PCDATA">
<!ENTITY % syntaxcontent "%simple;|synopt|synvar">
<!ENTITY % variable "%simple;|sub">
...
<!-- some entities are preformatted -->
<!ENTITY % space.att 'xml:space (default|preserve) #FIXED "preserve"'>
...
<!ELEMENT syntax (%syntaxcontent;)*>
<!ATTLIST syntax %space.att;>

<!ELEMENT synopt (%syntaxcontent;)*>
<!ELEMENT synvar (%variable;)*>

<!ELEMENT sub (%simple;)*>
=========================

A typical document might include the following:
========== XML ==========
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document SYSTEM "mydocs.dtd">
<document title="Testing Document">
...
<para>Type:</para>
<syntax>testdoc <synvar>switches</synvar> <synvar>file</synvar></syntax>
<para>at the command line, where:</para>
...
</document>
=========================

I have a stylesheet which translates this to HTML, like so:
========== XSL ==========
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   xmlns:xs="http://www.w3.org/2001/XMLSchema";>
<xsl:output method="html" encoding="ISO-8859-1"/>
...
<xsl:template match="syntax">
<div class="syntax">
<p><xsl:apply-templates /></p>
</div>
</xsl:template>
<xsl:template match="synopt">
[&#160;<xsl:apply-templates/>&#160;]
</xsl:template>
<xsl:template match="synvar">
<i><xsl:apply-templates /></i>
</xsl:template>
...
<!-- preserve spaces (and indentation) in syntax fragments-->
<xsl:template match="text()[ancestor::syntax]">
<xsl:value-of select="translate(.,' ','&#160;')" />
</xsl:template>
...
</xsl:stylesheet>
=========================

My problem is the space between the two <synvar> elements, which,
notwithstanding the xml:space="preserve" attribute on <syntax>, is being
stripped. When I run the standard "showtree.xsl" stylesheet on my document,
I see this output:
======== showtree ========
...
1.2.5  Element 'para' (document,section): 
1.2.5.1  Text (document,section,para): {Type:}
1.2.6  Element 'syntax' (document,section): 
1.2.6.A  Attribute '{http://www.w3.org/XML/1998/namespace}xml:space':
{preserve}
1.2.6.1  Text (document,section,syntax): {testdoc }
1.2.6.2  Element 'synvar' (document,section,syntax): 
1.2.6.2.1  Text (document,section,syntax,synvar): {switches}
1.2.6.3  Element 'synvar' (document,section,syntax): 
1.2.6.3.1  Text (document,section,syntax,synvar): {file}
1.2.7  Element 'para' (document,section): 
1.2.7.1  Text (document,section,para): {at the command line, where:}
...
==========================

So although <syntax> clearly has preserve space declared, the space between
the <synvar> elements does not show up. 

I may well have misunderstood how this is supposed to work, but so far as I
can tell I have followed examples in several reputable books, and the
results I am seeing are not what they led me to expect.

I have tried this with XMLSpy 2006 Home edition on WinXP and Saxon 8.6.3 on
Linux.

Where have I gone wrong?

Cheers
Trevor

Current Thread