Common Trim function for XSLT (as a template)
1
2 <xsl:template name="left-trim">
3 <xsl:param name="s" />
4 <xsl:choose>
5 <xsl:when test="substring($s, 1, 1) = ''">
6 <xsl:value-of select="$s"/>
7 </xsl:when>
8 <xsl:when test="normalize-space(substring($s, 1, 1)) = ''">
9 <xsl:call-template name="left-trim">
10 <xsl:with-param name="s" select="substring($s, 2)" />
11 </xsl:call-template>
12 </xsl:when>
13 <xsl:otherwise>
14 <xsl:value-of select="$s" />
15 </xsl:otherwise>
16 </xsl:choose>
17 </xsl:template>
18
19 <xsl:template name="right-trim">
20 <xsl:param name="s" />
21 <xsl:choose>
22 <xsl:when test="substring($s, 1, 1) = ''">
23 <xsl:value-of select="$s"/>
24 </xsl:when>
25 <xsl:when test="normalize-space(substring($s, string-length($s))) = ''">
26 <xsl:call-template name="right-trim">
27 <xsl:with-param name="s" select="substring($s, 1, string-length($s) - 1)" />
28 </xsl:call-template>
29 </xsl:when>
30 <xsl:otherwise>
31 <xsl:value-of select="$s" />
32 </xsl:otherwise>
33 </xsl:choose>
34 </xsl:template>
35
36 <xsl:template name="trim">
37 <xsl:param name="s" />
38 <xsl:call-template name="right-trim">
39 <xsl:with-param name="s">
40 <xsl:call-template name="left-trim">
41 <xsl:with-param name="s" select="$s" />
42 </xsl:call-template>
43 </xsl:with-param>
44 </xsl:call-template>
45 </xsl:template>