[Home] [By Thread] [By Date] [Recent Entries]
Hi
I want to capitalise the first letter of a string in XSLT 1.0. Now, my solution to this seemingly simple problem is this: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:myns="uri.my.namespace" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" /> <xsl:template match="/"> <xsl:call-template name="find_char_position"> <xsl:with-param name="in_string">abcdefghijklmnopqrstuvwxyz</xsl:with-param> <xsl:with-param name="char">A</xsl:with-param> </xsl:call-template> <xsl:text> </xsl:text> <xsl:call-template name="find_char_position"> <xsl:with-param name="in_string">abcdefghijklmnopqrstuvwxyz</xsl:with-param> <xsl:with-param name="char"></xsl:with-param> </xsl:call-template> <xsl:text> </xsl:text> <xsl:call-template name="capitalise"> <xsl:with-param name="string">kamal</xsl:with-param> </xsl:call-template> <xsl:text> </xsl:text> <xsl:call-template name="capitalise"> <xsl:with-param name="string">Kamal</xsl:with-param> </xsl:call-template> <xsl:text> </xsl:text> <xsl:call-template name="capitalise"> <xsl:with-param name="string"></xsl:with-param> </xsl:call-template> <xsl:text> </xsl:text> <xsl:call-template name="capitalise"> <xsl:with-param name="string">1amal</xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template name="capitalise"> <xsl:param name="string"/> <xsl:variable name="lowercase_alphabet"> <xsl:text>abcdefghijklmnopqrstuvwxyz</xsl:text> </xsl:variable> <xsl:variable name="uppercase_alphabet"> <xsl:text>ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:text> </xsl:variable> <xsl:variable name="letter_pos"> <xsl:call-template name="find_char_position"> <xsl:with-param name="in_string" select="$lowercase_alphabet"/> <xsl:with-param name="char" select="substring($string, '1', '1')"/> </xsl:call-template> </xsl:variable> <xsl:choose> <xsl:when test="$letter_pos = -1"><xsl:value-of select="$string"/></xsl:when> <xsl:otherwise><xsl:value-of select="concat(substring($uppercase_alphabet, $letter_pos, 1), substring($string, 2))"/></xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="find_char_position"> <xsl:param name="in_string"/> <xsl:param name="char"/> <xsl:choose> <xsl:when test="not(contains($in_string, $char)) or normalize-space($char) = ''">-1</xsl:when> <xsl:otherwise> <xsl:call-template name="find_char_position_rec"> <xsl:with-param name="pos">1</xsl:with-param> <xsl:with-param name="in_string" select="$in_string"/> <xsl:with-param name="char" select="$char"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="find_char_position_rec"> <xsl:param name="pos"/> <xsl:param name="in_string"/> <xsl:param name="char"/> <xsl:choose> <xsl:when test="starts-with(substring($in_string, $pos), $char)"><xsl:value-of select="$pos"/></xsl:when> <xsl:otherwise> <xsl:call-template name="find_char_position_rec"> <xsl:with-param name="in_string" select="$in_string"/> <xsl:with-param name="pos" select="$pos + 1"/> <xsl:with-param name="char" select="$char"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> Put aside the testing, this is just too long. Any ideas how I can simplify this solution? I have access to exslt functions. Cheers. -- Kamal Bhatt
|

Cart



