[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Getting a list of node Text()


Subject: Re: [xsl] Getting a list of node Text()
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 20 Jun 2007 16:56:39 +0200

Nicholas Orr wrote:

Yes, I agree. The source I have no control over, other than to submit error reports and hope for the best. It's coming out of an app where I don't control the output.


But having investigated this I came to the same conclusion, and I'll submit some reports about it and see what happens.

Nicholas, if you draw a blank on your error reports, i.e., if you *have* to remove the whitespace yourself, consider the following three options:


1. The FAQ entry: http://www.dpawson.co.uk/xsl/sect2/N8321.html#d11899e830
2. One of the two solutions below, which is rather straightforward, the second admittedly being a bit obfuscated while trying to minimize the code
3. XSLT 2 (convince your bosses to upgrade): replace($text, '\s*([^\s]+)\s*', '$1')


<!-- solution ONE, easy to read, but a bit verbose -->
   <xsl:template name="strip-ws">
       <xsl:param name="text" />

<xsl:variable name="end" select="substring($text, string-length($text))" />
<xsl:variable name="start" select="substring($text, 1, 1)" />


<xsl:choose>
<xsl:when test="normalize-space($start) = ''">
<xsl:call-template name="strip-ws">
<xsl:with-param name="text" select="substring($text, 2)" />
</xsl:call-template>
</xsl:when>
<xsl:when test="normalize-space($start) = ''">
<xsl:call-template name="strip-ws">
<xsl:with-param name="text" select="substring($text, 1, string-length($text) - 1)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<!-- solution TWO, less verbose, uses booleans as count, less verbose, but harder to read -->
<xsl:template name="strip-ws">
<xsl:param name="text" />


<xsl:variable name="test-start" select="normalize-space(substring($text, 1, 1)) = ''" />
<xsl:variable name="test-end" select="normalize-space(substring($text, string-length($text))) = ''" />


<xsl:if test="$test-start or $test-end">
<xsl:call-template name="strip-ws">
<xsl:with-param name="text" select="substring($text, $test-start + 1, string-length($text) - $test-end)" />
</xsl:call-template>
</xsl:if>
<xsl:if test="not($test-start or $test-end)">
<xsl:value-of select="$text"/>
</xsl:if>


</xsl:template>


HTH, Cheers, -- Abel


Current Thread
Keywords