Use XSL to translate string to Upper Lower Case

139 views
Skip to first unread message

dperkalis

unread,
Oct 31, 2008, 3:21:45 PM10/31/08
to xml and xslt
I recently had the need to convert an all CAPS string to UpperLower
Case. ie. THIS IS MY STRING -> This Is My String.

I couldn't find anything to do it in XSL 1.0, so I wrote the following
if anyone finds it helpful:

<xsl:template name="ToUpperLowerCase">
<xsl:param name="inputString"/>
<xsl:param name="index"/>
<xsl:param name="loopCount"/>
<xsl:if test="$index &lt; $loopCount">
<xsl:choose>
<xsl:when test="substring($inputString, ($index)-1,1) = ' ' or
$index = 1">
<xsl:call-template name="ToUpper">
<xsl:with-param name="inputString"
select="substring($inputString,$index,1)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="ToLower">
<xsl:with-param name="inputString"
select="substring($inputString,$index,1)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="ToUpperLowerCase">
<xsl:with-param name="inputString" select="$inputString"/>
<xsl:with-param name="index" select="($index)+1"/>
<xsl:with-param name="loopCount" select="$loopCount"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template name="ToLower">
<xsl:param name="inputString"/>
<xsl:variable name="smallCase" select="'abcdefghijklmnopqrstuvwxyz'"/
>
<xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/
>
<xsl:value-of select="translate($inputString,$upperCase,
$smallCase)"/>
</xsl:template>

<xsl:template name="ToUpper">
<xsl:param name="inputString"/>
<xsl:variable name="smallCase" select="'abcdefghijklmnopqrstuvwxyz'"/
>
<xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/
>
<xsl:value-of select="translate($inputString,$smallCase,$upperCase)"/
>
</xsl:template>


You can call it using the following:

<xsl:call-template name="ToUpperLowerCase">
<xsl:with-param name="inputString" select="ValueToConvert"/>
<xsl:with-param name="index" select="0"/>
<xsl:with-param name="loopCount" select="string-
length(ValueToConvert)+1"/>
</xsl:call-template>

uday dubey

unread,
Aug 30, 2013, 6:03:13 AM8/30/13
to xml-an...@googlegroups.com
Thanks, It is very usefull... 
 
Reply all
Reply to author
Forward
0 new messages