RE: [xsl] Question on preceding Text()

Subject: RE: [xsl] Question on preceding Text()
From: "Michael Kay" <mike@xxxxxxxxxxxx>
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:

     match="Center//text()"


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

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

 and 
> 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
http://www.saxonica.com/

Current Thread