XSLT 1.0 How to make text data Title Case Hmmm….. Interesting…..

Here is the solution it is messy but works for XSLT 1.0

I am getting late so straight away going for quick example

————————-
test.xml
————————-

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xslt" ?>
<data>
<input>
bhumip safi
</input>
</data>

————————-
test.xslt
————————-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<!--- Consider Below As Function -->
<xsl:template name="ConvertTitleCase">
<xsl:param name="temp" />
<xsl:call-template name="TitleCase">
<xsl:with-param name="text" select="translate(normalize-space($temp), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:call-template>
</xsl:template>
<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=' ' or $lastletter='/' or $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>

<!--- Consider Below As Caller Of Function -->
<xsl:template match="/">
<xsl:apply-templates select="data" />
</xsl:template>
<xsl:template match="data">
<output>
<xsl:call-template name="ConvertTitleCase">
<xsl:with-param name="temp" select="input" />
</xsl:call-template>
</output>
</xsl:template>

</xsl:stylesheet>

How every need to code much compare to other languages but
Wallah — problem solved

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s