<xsl:template name="trim">
    <xsl:param name="string"/>
 
    <xsl:variable name="lTrim">
        <xsl:choose>
            <xsl:when test="starts-with($string, ' ')"><xsl:value-of select="substring($string, 2)"/></xsl:when>
            <xsl:otherwise><xsl:value-of select="$string"/></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
 
    <xsl:variable name="rTrim">
        <xsl:choose>
            <xsl:when test="substring(string-length($lTrim), 1) = ' '"><xsl:value-of select="substring($lTrim, 1, string-length($lTrim)-1)"/></xsl:when>
            <xsl:otherwise><xsl:value-of select="$lTrim"/></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
 
    <xsl:choose>
        <xsl:when test="starts-with($rTrim, ' ') or substring(string-length($rTrim), 1) = ' '">
            <xsl:call-template name="trim">
                <xsl:with-param name="string"><xsl:value-of select="$rTrim"/></xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="$rTrim"/></xsl:otherwise>
    </xsl:choose>
</xsl:template>

Trim String in XSLT

April 03, 2008
I wrote this small template to remove (trim) leading and training space characters from strings using XSL. Pass in a string to trim as a parameter and get a result.