Fwd: Re: [xsl] Xsl:strip-space and xsl:preserve-space (XSLT 2.0)

Subject: Fwd: Re: [xsl] Xsl:strip-space and xsl:preserve-space (XSLT 2.0)
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 24 Sep 2008 09:01:04 -0700
(retry due to mailer problems from yesterday)

Date: Tue, 23 Sep 2008 16:55:55 -0700
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Subject: Re: [xsl] Xsl:strip-space and xsl:preserve-space (XSLT 2.0)

At 2008-09-24 04:21 +0530, Pankaj Chaturvedi wrote:
In my stylesheet, I am stripping all the spaces, new line character while
using <xsl:strip-space elements="*"/> which works perfectly as desired.

But have you confirmed that without this statement this is not already happening?


You don't mention which processor you are using or what invocation parameters you are using.

In XSLT 1.0 you could rely on the source document white-space-only text nodes to be preserved (except when using Microsoft). In XSLT 2.0 you cannot rely on this, because the *invocation* of the XSLT processor can change whether or not white-space-only text nodes in source documents are preserved.

Note, for example, the invocation of Saxon has the following parameter:

-strip:all|none|ignorable

Here is an example where the white-space between "</journal-title>" and "<first-page>" is shown to be stripped or preserved without saying anything in the stylesheet about stripping:

T:\ftemp>type pankaj.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:output method="text"/>

</xsl:stylesheet>
T:\ftemp>type pankaj.xml
<element1><surname>Dajnoki</surname>, <givenname>K.</givenname>
(<year>2002</year>) <article-title>XXXXX YYYY</article-title>.
<journal-title>ZZZZZZ XXXX</journal-title>
<first-page>43</first-page>-<last-page>46</last-page>.</element1>

T:\ftemp>saxon9 -strip:all pankaj.xml pankaj.xsl
Dajnoki, K.
(2002) XXXXX YYYY.
ZZZZZZ XXXX43-46.
T:\ftemp>saxon9 -strip:none pankaj.xml pankaj.xsl
Dajnoki, K.
(2002) XXXXX YYYY.
ZZZZZZ XXXX
43-46.
T:\ftemp>

But now I've come across the requirement where I need to preserve spaces
between the tags in few specific elements generally the punctuations(see
below example) I tried to use the <xsl:preserve-space> to sort out this
issue but doesn't seems to be working

Perhaps that is because the data model for your document, as created by your stylesheet processor, is already stripping the white-space-only text nodes before your stylesheet is invoked.


and "xsl:strip-space" seems to
overcoming the effect of "xsl:preserve-space" effects. Can we use both to
get their respective effects or simply they cannot be used together?

They can be used together.


Consider the following examples where, in pankaj2.xsl all elements are having their white-space-only text nodes stripped with <xsl:strip-space/> but in pankaj3.xsl there is the override with <xsl:preserve-space/>:

T:\ftemp>type pankaj2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:strip-space elements="*"/>

<xsl:output method="text"/>

</xsl:stylesheet>
T:\ftemp>saxon9 -strip:none pankaj.xml pankaj2.xsl
Dajnoki, K.
(2002) XXXXX YYYY.
ZZZZZZ XXXX43-46.
T:\ftemp>type pankaj3.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:strip-space elements="*"/>

<xsl:preserve-space elements="element1 element2"/>

<xsl:output method="text"/>

</xsl:stylesheet>
T:\ftemp>saxon9 -strip:none pankaj.xml pankaj3.xsl
Dajnoki, K.
(2002) XXXXX YYYY.
ZZZZZZ XXXX
43-46.
T:\ftemp>

Note: Spaces between the tags with any punctuation appears correctly (like
in <surname>Dajnoki</surname>, <givenname>K.</givenname>)  but fails in case
of a single space between tags > < (for instance in </journal-title>
<first-page> and results into the </journal-title><first-page> )

Stripping and preserving white-space-only text nodes do not impact text nodes that are not white-space-only. When there is punctuation the node is not white-space-only.

I hope this helps.

. . . . . . . . . . Ken


-- Upcoming XSLT/XSL-FO hands-on courses: Wellington, NZ 2009-01 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread