RE: [xsl] Question on preceding Text()

Subject: RE: [xsl] Question on preceding Text()
From: "Besi Fube" <pearle20@xxxxxxxxxxx>
Date: Tue, 12 Apr 2005 14:01:18 -0400
Thanks for your feedback it was very helpful.
On the xslt, the statment:
was valid only when test="../parent::Center" .
I was not accounting for a case where test="parent::Center"
in which case I also needed to check for
based on my tree structure.

Thanks again,

----Original Message Follows----
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE: [xsl] Question on preceding Text()
Date: Tue, 12 Apr 2005 16:48:15 +0100

> The problem I seem to be having with this is that text() for
> instance the
> text "Chapter on Testing:" can not be considered a
> preceding-sibling of
> <AddText>Testing FAQ</AddText> even though they have the same
> <Center>
> parent tag since preceding-sibling does not grab the text node.

In your example the text node "Chapter on Testing:" is indeed a
preceding-sibling of the element node <AddText>Testing FAQ</AddText>,
so the problem must be somewhere else.

Note that preceding-sibling::* only selects elements, to select text nodes
use preceding-sibling::text(), and to select both using
preceding-sibling::node(). You probably only want the immediately preceding
sibling, which is preceding-sibling::node()[1].

However, if you select a text node then its string-length will always be >0.
Zero-length text nodes do not exist. Perhaps you really want to test for the
existence of the text node rather than its length? Fortunately, though,
string-length() applied to an empty node-set returns 0.

> <xsl:template match="text()">
> 	<xsl:if test="ancestor::Center">

Better to incorporate that in the match pattern:


> <xsl:choose> > <xsl:when test ="name(../..)='Center'

Better to write that as test="../parent::Center"

> string-length(parent::*/preceding-sibling::*)>0">
> 				<xsl:text> </xsl:text>
> 			</xsl:when>
> 			<xsl:otherwise>
> 				<xsl:text
> disable-output-escaping='yes'>&lt;center&gt;</xsl:text>

As DC explained, d-o-e is not the right way to do things.

Michael Kay

Current Thread