Custom Search
Logiclabz
  • Home
  • XML/XSLT
  • Title case (Proper case) template/function in XSLT

Title case (Proper case) template/function in XSLT

This following template in xslt can be used to change text to its title case:

<xsl:template name="TitleCase">
<xsl:param name="text" />
<xsl:param name="lastletter" select="' '"/>
<xsl:if test="$text">
<xsl:variable name="thisletter" select="substring($text,1,1)"/>
<xsl:choose>
<xsl:when test="$lastletter=' '">
<xsl:value-of select="translate($thisletter,'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$thisletter"/>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="TitleCase">
<xsl:with-param name="text" select="substring($text,2)"/>
<xsl:with-param name="lastletter" select="$thisletter"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

And it can be called in other template as below:

<xsl:variable name="CatName">
<xsl:call-template name="TitleCase">
<xsl:with-param name="text" select="translate(normalize-space($ypcategoryname),
'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:call-template>
</xsl:variable>


Comments

  • Jasmine Myer says:
    Jul 16, 09

    Great. Was able to modify this for my own purposes. Much appreciated - thanks.

  • bjawnie says:
    Jul 17, 09

    Thanks, this little snippet just saved me a lot of work. Cheers

  • Pete Danes says:
    Nov 16, 09

    Great, I'm new to XSLT and flailing a bit. This helped me get something done and helped me learn.

  • brenda says:
    Dec 29, 09

    This is what I need, but I can't get it to display the values. Can you please help?


Leave a reply


Do you like this post?