This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: preserving whitespace


> I have looked at the archives and failed to implement the 
> xml:space="preserve"
> to preserve white space in selected nodes - failing that preserving
> whitespace globally.
> 
> I have tried most things I could think of, so would appreciate a
> simple example of how to achieve this.

The following stylesheet (applyed on any xml source) will try to pad the string
"abc" with 3 spaces to the right.

Two templates -- "pad" and "pad2" are called, the only difference being in how the
"space buffer" variable is defined in them:

In the "pad" template we have:

    <xsl:variable name="spaces">
	    <xsl:text xml:space="preserve">          </xsl:text>
    </xsl:variable>

In the "pad2" template we have:

    <xsl:variable name="spaces">
	    <xsl:text>          </xsl:text>
    </xsl:variable>

When applied with MSXML3 (which eats-out all whitespace-only text nodes by default,
unless there is an xml:space="preserve" attribute specified on an ancestor node) the
result is:

<pre>'abc   '</pre>
<pre>'abc '</pre>

The second try (the output of the call to the "pad2" template), did not succeed in
padding the string "abc" with 3 more spaces, simply because the "spaces" variable
was not initialised with the 10 spaces child of xsl:text -- this child node had been
simply stripped by the MSXML3 parser in producing the DOM tree.

In case you run this transformation with Saxon, because its parser does not strip by
default whitespace-only text nodes, the result is the same on both calls:

<pre>'abc   '</pre><pre>'abc   '</pre>

Possibly you couldn't find any difference in the results of your transformation when
xml:space="preserve" was used, because the parser you were using was not stripping
whitespace-only nodes by default as is the case with Saxon.

Here's the stylesheet:

<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
>
 <xsl:output method="html"/>

<xsl:template match="/">
	<pre>'<xsl:call-template name="pad">
		<xsl:with-param name="String" select="'abc'"/>
		<xsl:with-param name="length" select="6"/>
	</xsl:call-template>'</pre>

	<pre>'<xsl:call-template name="pad2">
		<xsl:with-param name="String" select="'abc'"/>
		<xsl:with-param name="length" select="6"/>
	</xsl:call-template>'</pre>
</xsl:template>

<xsl:template name="pad">
    <xsl:param name="String"/>
    <xsl:param name="length"/>
    <xsl:variable name="spaces">
	    <xsl:text xml:space="preserve">          </xsl:text>
    </xsl:variable>
    <xsl:value-of select="substring(concat($String,$spaces),1,$length)"/>
</xsl:template>

<xsl:template name="pad2">
    <xsl:param name="String"/>
    <xsl:param name="length"/>
    <xsl:variable name="spaces">
	    <xsl:text>          </xsl:text>
    </xsl:variable>
    <xsl:value-of select="substring(concat($String,$spaces),1,$length)"/>
</xsl:template>

</xsl:stylesheet>

Hope this helped.

Cheers,
Dimitre Novatchev.



__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger. http://im.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]