Convert the first character of each word to uppercase

I have a String and I need to convert the first letter of each word to uppercase and rest in lowercase using xsl. For example,

Input String = dInEsh sAchdeV kApil Muk

Desired output line = Dinesh Sachdev Kapil Muk

Although I know that I need to use the translation function for this purpose, but how can I translate the first chart of each word into upper case and everyone else in lower case using XSLT 1.0

thanks

+7
source share
7 answers

The following is not β€œnice,” and I'm sure someone (mostly Demetrius) might come up with something much simpler (especially in XSLT 2.0) ... but I checked it and it works

<xsl:template name="CamelCase"> <xsl:param name="text"/> <xsl:choose> <xsl:when test="contains($text,' ')"> <xsl:call-template name="CamelCaseWord"> <xsl:with-param name="text" select="substring-before($text,' ')"/> </xsl:call-template> <xsl:text> </xsl:text> <xsl:call-template name="CamelCase"> <xsl:with-param name="text" select="substring-after($text,' ')"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:call-template name="CamelCaseWord"> <xsl:with-param name="text" select="$text"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="CamelCaseWord"> <xsl:param name="text"/> <xsl:value-of select="translate(substring($text,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /><xsl:value-of select="translate(substring($text,2,string-length($text)-1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" /> </xsl:template> 

The basic idea is that you call CamelCase if it finds a space, then it launches CamelCaseWord on everything before the space (i.e. the first word), and then calls CamelCase again with everything after the space (i.e. the rest of the sentence ) Otherwise, if the space is not found (because it got to the last word in the sentence), then it just calls CamelCaseWord .

The CamelCaseWord template simply translates the first character from the bottom to the top (if necessary) and all other characters from the top to the bottom (if necessary).

So, to call it, you would ...

 <xsl:call-template name="CamelCase"> <xsl:with-param name="text">dInEsh sAchdeV kApil Muk</xsl:with-param> </xsl:call-template> 
+10
source

Additionally:

I missed requirement 1.0 in the question. This will only work with version 2.0.

Original answer below.

I believe this worked for me some time ago. Declare a function:

 <xsl:function name="my:titleCase" as="xs:string"> <xsl:param name="s" as="xs:string"/> <xsl:choose> <xsl:when test="lower-case($s)=('and','or')"> <xsl:value-of select="lower-case($s)"/> </xsl:when> <xsl:when test="$s=upper-case($s)"> <xsl:value-of select="$s"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="concat(upper-case(substring($s, 1, 1)), lower-case(substring($s, 2)))"/> </xsl:otherwise> </xsl:choose> </xsl:function> 

And use it:

 <xsl:sequence select="string-join(for $x in tokenize($text,'\s') return my:titleCase($x),' ')"/> 

the loan goes to samjudson => http://p2p.wrox.com/xslt/80938-title-case-string.html

+3
source

Here is an 8 year old FXSL 1.x (XSLT 1.0 libray, completely written in XSLT 1.0):

test strSplit to Words10.xsl:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common" > <xsl:import href="strSplitWordDel.xsl"/> <!-- To be applied on: test-strSplit-to-Words10.xml --> <xsl:output indent="yes" omit-xml-declaration="yes"/> <xsl:variable name="vLower" select="'abcdefgijklmnopqrstuvwxyz'"/> <xsl:variable name="vUpper" select="'ABCDEFGIJKLMNOPQRSTUVWXYZ'"/> <xsl:template match="/"> <xsl:variable name="vwordNodes"> <xsl:call-template name="str-split-word-del"> <xsl:with-param name="pStr" select="/"/> <xsl:with-param name="pDelimiters" select="', .(&#9;&#10;&#13;'"/> </xsl:call-template> </xsl:variable> <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/> </xsl:template> <xsl:template match="word"> <xsl:choose> <xsl:when test="not(position() = last())"> <xsl:value-of select="translate(substring(.,1,1),$vLower,$vUpper)"/> <xsl:value-of select="substring(.,2)"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="delim"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> 

When this conversion is applied to the following XML document (test-strSplit-to-Words10.xml):

 <t>004.lightning crashes (live).mp3</t> 

result :

 004.Lightning Crashes (Live).mp3 

For this XML document (provided sample):

 dInEsh sAchdeV kApil Muk 

result :

 dInEsh sAchdeV kApil Muk 

With a little tweek we get this code :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common" > <xsl:import href="strSplitWordDel.xsl"/> <!-- To be applied on: test-strSplit-to-Words10.xml --> <xsl:output indent="yes" omit-xml-declaration="yes"/> <xsl:variable name="vLower" select="'abcdefgijklmnopqrstuvwxyz'"/> <xsl:variable name="vUpper" select="'ABCDEFGIJKLMNOPQRSTUVWXYZ'"/> <xsl:template match="/"> <xsl:variable name="vwordNodes"> <xsl:call-template name="str-split-word-del"> <xsl:with-param name="pStr" select="/"/> <xsl:with-param name="pDelimiters" select="', .(&#9;&#10;&#13;'"/> </xsl:call-template> </xsl:variable> <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/> </xsl:template> <xsl:template match="word"> <xsl:value-of select="translate(substring(.,1,1),$vLower,$vUpper)"/> <xsl:value-of select="translate(substring(.,2), $vUpper, $vLower)"/> </xsl:template> <xsl:template match="delim"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> 

which now gives the desired result :

 dInEsh sAchdeV kApil Muk 

Explanation

The str-split-word-del FXSL template can be used to tokenize (possibly more than one) the delimiters specified as a string parameter.

+3
source

You can also try the following:

http://www.xsltfunctions.com/xsl/functx_camel-case-to-words.html

  <xsl:function name="functx:camel-case-to-words" as="xs:string" xmlns:functx="http://www.functx.com"> <xsl:param name="arg" as="xs:string?"/> <xsl:param name="delim" as="xs:string"/> <xsl:sequence select=" concat(substring($arg,1,1), replace(substring($arg,2),'(\p{Lu})', concat($delim, '$1'))) "/> </xsl:function> 

and back: http://www.xsltfunctions.com/xsl/functx_words-to-camel-case.html

 <xsl:function name="functx:words-to-camel-case" as="xs:string" xmlns:functx="http://www.functx.com"> <xsl:param name="arg" as="xs:string?"/> <xsl:sequence select=" string-join((tokenize($arg,'\s+')[1], for $word in tokenize($arg,'\s+')[position() > 1] return functx:capitalize-first($word)) ,'') "/> </xsl:function> 
+2
source

Here is another short solution. It uses pure XSL-T 2.0. I know that the OP has a requirement for XSL-T 1.0, but since this page ranks first on Google for the "xsl-t title case" function in 2015, this looks more relevant:

 <xsl:function name="xx:fixCase"> <xsl:param name="text" /> <xsl:for-each select="tokenize($text,' ')"> <xsl:value-of select="upper-case(substring(.,1,1))" /> <xsl:value-of select="lower-case(substring(.,2))" /> <xsl:if test="position() ne last()"> <xsl:text> </xsl:text> </xsl:if> </xsl:for-each> </xsl:function> 

Where "xx" is your own namespace.

+1
source

The same function on XQUERY:

Xquery function for camel case.

 declare function xf:toCamelCase($text as xs:string?) as xs:string{ if(contains($text,' ')) then fn:concat(xf:CamelCaseWord(substring-before($text,' ')),' ', xf:toCamelCase(substring-after($text,' '))) else xf:CamelCaseWord($text) }; declare function xf:CamelCaseWord($text as xs:string?) as xs:string{ fn:concat( translate(substring($text,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'), translate(substring($text,2,string-length($text)-1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')) }; 
0
source

A very short solution using the EXSLT split() function:

 <xsl:variable name='text' select='"dInEsh sAchdeV kApil Muk"' /> <xsl:variable name='lowers' select='"abcdefghijklmnopqrstuvwxyz"' /> <xsl:variable name='uppers' select='"ABCDEFGHIJKLMNOPQRSTUVWXYZ"' /> <xsl:template match="/"> <xsl:for-each select='str:split($text, " ")'> <xsl:value-of select='concat( translate(substring(., 1, 1), $lowers, $uppers), translate(substring(., 2), $uppers, $lowers), " " )' /> </xsl:for-each> </xsl:template> 

Working demo: http://www.xmlplayground.com/CNmKdF

0
source

All Articles