Since XSL 1.0 dows not have inbuild Trimming functions for Strings, you need to define your own templates.
You can use the Trim Templates shown in the codesnippet below.

Sample XSL

<xsl:variable name="whitespaceCharacters" select="'&#09;&#10;&#13; '" />
    
    <!-- Trim Right side of the String -->
    <xsl:template name="TrimRight">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        
        <xsl:variable name="length" select="string-length($input)" />
        <xsl:if test="string-length($input) &gt; 0">
            <xsl:choose>
                <xsl:when test="contains($trim, substring($input, $length, 1))">
                    <xsl:call-template name="TrimRight">
                        <xsl:with-param name="input" select="substring($input, 1, $length - 1)" />
                        <xsl:with-param name="trim" select="$trim" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$input" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    
    <!-- Trim Left side of the String -->
    <xsl:template name="TrimLeft">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        
        <xsl:if test="string-length($input) &gt; 0">
            <xsl:choose>
                <xsl:when test="contains($trim, substring($input, 1, 1))">
                    <xsl:call-template name="TrimLeft">
                        <xsl:with-param name="input" select="substring($input, 2)" />
                        <xsl:with-param name="trim" select="$trim" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$input" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    
    <!-- Trim both sides of the String -->
    <xsl:template name="Trim">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        <xsl:call-template name="TrimRight">
            <xsl:with-param name="input">
                <xsl:call-template name="TrimLeft">
                    <xsl:with-param name="input" select="$input" />
                    <xsl:with-param name="trim" select="$trim" />
                </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name="trim" select="$trim" />
        </xsl:call-template>
    </xsl:template>

4 thought on “How to trim a string using XSL 1.0”

Leave a Reply