RE: [xsl] Preserve-space isn't

Subject: RE: [xsl] Preserve-space isn't
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 31 Mar 2006 09:30:22 +0100
When I take your input files and remove the "..." bits to make them well
formed, and then run using

java -jar c:\MyJava\saxon8.jar -t test.xml test.xsl

I get 

Saxon 8.7J from Saxonica
Java version 1.5.0_06
...

...
Type:
<div xmlns:xs="http://www.w3.org/2001/XMLSchema"; class="syntax">
   <p>testdoc&nbsp;<i>switches</i>&nbsp;<i>file</i></p>
</div>
at the command line, where:
...

which looks correct to me.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Trevor Nicholls [mailto:trevor@xxxxxxxxxxxxxxxxxx] 
> Sent: 31 March 2006 06:32
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Preserve-space isn't
> 
> 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