Saturday, 17 December 2011
XSLT Title Case Converter
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:griffmonster="http://www.griffmonster.com/functions" exclude-result-prefixes="griffmonster">
<!-- title case converter module
takes a string input and converts to title case as output
uses an exceptions file for word exceptions
created griffmonster -->
<xsl:param name="strExceptionsFilename">titleCaseExceptions.xml</xsl:param>
<xsl:param name="nstExceptions" select="document($strExceptionsFilename)"/>
<xsl:function name="griffmonster:fnTitleCase">
<xsl:param name="string"/>
<xsl:variable name="intLength" select="string-length($string)"/>
<xsl:variable name="tokenizedSample" select="tokenize($string,'\s')"/>
<xsl:for-each select="$tokenizedSample">
<xsl:variable name="strToken" select="."/>
<xsl:choose>
<xsl:when test="$nstExceptions//Exception[lower-case(self::*) = lower-case($strToken)]">
<xsl:value-of select="$nstExceptions//Exception[lower-case(self::*) = lower-case($strToken)]"/>
</xsl:when>
<!--Filter for name begining with O' -->
<xsl:when test='starts-with(lower-case($strToken),"o'")'>
<xsl:value-of select="upper-case(substring($strToken, 1, 3))"/>
<xsl:value-of select="lower-case(substring($strToken, 4, $intLength))"/>
</xsl:when>
<!-- filter for names begining with 'Mc' -->
<xsl:when test="starts-with(lower-case($strToken),'mc')">
<xsl:value-of select="upper-case(substring($strToken, 1, 1))"/>
<xsl:value-of select="lower-case(substring($strToken, 2, 1))"/>
<xsl:value-of select="upper-case(substring($strToken, 3, 1))"/>
<xsl:value-of select="lower-case(substring($strToken, 4, $intLength))"/>
</xsl:when>
<!--filter for hyphenated names-->
<xsl:when test="contains(lower-case($strToken),'-')">
<xsl:variable name="tokenizedHyphen" select="tokenize($strToken,'-')"/>
<xsl:for-each select="$tokenizedHyphen">
<xsl:value-of select="upper-case(substring(., 1, 1))"/>
<xsl:value-of select="lower-case(substring(., 2))"/>
<xsl:if test="position() != last()">
<xsl:text>-</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:when>
<!--filter for postcodes-->
<xsl:when test="matches($strToken,'[A-Z][A-Z]?[0-9][0-9]?') or matches($strToken,'[0-9][A-Z]{2}')">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="upper-case(substring($strToken, 1, 1))"/>
<xsl:value-of select="lower-case(substring($strToken, 2, $intLength))"/>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="position() != last() and matches($strToken,'[A-Z][A-Z]?[0-9][0-9]?')">
<xsl:text> </xsl:text>
</xsl:when>
<xsl:when test="position() != last()">
<xsl:text> </xsl:text>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:function>
</xsl:stylesheet>
<!-- Exceptions file can be added thus
<Exceptions>
<Exception description="Example Company Ltd">ECL</Exception>
<Exception>GriffMonster</Exception>
<Exception description="acronym for Limited Liability Partnership">LLP</Exception>
<Exception>UK</Exception>
<Exception>in</Exception>
<Exception>aka</Exception>
</Exceptions>
Subscribe to:
Posts (Atom)