Re: [xsl] removing final space from node tree

Subject: Re: [xsl] removing final space from node tree
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Mon, 20 Apr 2009 23:07:07 +0100
>  <!-- We need to remove any extraneous final spaces. -->
>  <!-- set context node to language-desc -->
>  <xsl:for-each select="$language-desc">
>    <!-- find last text node in descendants -->
>    <xsl:variable name="last-text-node" select="(.//text())[last()]"/>
>      <xsl:choose>
>        <xsl:when test="ends-with($last-text-node, ' ')">
>          <xsl:apply-templates mode="strip-final-space" select=".">
>            <xsl:with-param name="last-text-node"
>                select="$last-text-node"/>
>            </xsl:apply-templates>
>        </xsl:when>
>        <xsl:otherwise><xsl:sequence select="."/></xsl:otherwise>
>      </xsl:choose>
>    </xsl:for-each>
>
> ...
>
>    <!-- copy the context node tree, removing final space from the node
> that is passed as a parameter. -->
>    <xsl:template mode="strip-final-space" match="@*|node()">
>        <xsl:param name="last-text-node" />
>        <xsl:choose>
>            <xsl:when test=". is $last-text-node"><xsl:value-of
> select="substring(., 1, string-length(.) - 1)"/></xsl:when>
>            <xsl:otherwise>
>                <xsl:copy>
>                    <xsl:apply-templates mode="strip-final-space"
> select="@*|node()">
>                        <xsl:with-param name="last-text-node"
> select="$last-text-node"/>
>                    </xsl:apply-templates>
>                </xsl:copy>
>            </xsl:otherwise>
>        </xsl:choose>
>    </xsl:template>


I think this would do the same job:

<xsl:template match="text()">
  <xsl:value-of select="if (. is
/path/to/lanuguage-desc/descendant::text()[last()][ends-with(., ' ')])
then
      substring(., 1, string-length(.) - 1) else ."/>
</xsl:template>



--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread