Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 16th, 2010, 08:56 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Title Case a string

Hi, I have a template that takes a string and makes it title case. for example:

"the man and the moon (MATM)"

will display:

"The Man And The Moon (Matm)"

I need to be able to ignore contractions (and, or) and acronyms (MATM)

I would like the output to be:

"The Man and The Moon (MATM)"

Here is my current template:

Code:
<xsl:template name="titlecase">
    <xsl:param name="text" as="xs:string"/>
    <xsl:sequence select="string-join(for $x in tokenize($text,'\s') return concat(upper-case(substring($x, 1, 1)), lower-case(substring($x, 2))), ' ')"/>
</xsl:template>
I am not sure whats the best approach.

Thanks for the help.
 
Old September 16th, 2010, 09:15 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The easiest way would be to define a function and pass each string in to that. Then see if the string matches either one of your contractions, or is already all uppercase, and if so return unchanged.

Code:
   <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>
Then use it like so:

Code:
<xsl:sequence select="string-join(for $x in tokenize($text,'\s') return my:titleCase($x),' ')"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
bonekrusher (September 16th, 2010)
 
Old September 16th, 2010, 09:19 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I tried it with analyze-string:
Code:
  <xsl:function name="mf:titlecase" as="xs:string">
    <xsl:param name="text" as="xs:string"/>
    <xsl:param name="ignore-list" as="xs:string*"/>
    <xsl:value-of>
      <xsl:analyze-string select="$text" regex="\w+">
        <xsl:matching-substring>
          <xsl:analyze-string select="." regex="{string-join($ignore-list, '|')}">
            <xsl:matching-substring>
              <xsl:value-of select="."/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
              <xsl:sequence select="concat(upper-case(substring(., 1, 1)), lower-case(substring(., 2)))"/>
            </xsl:non-matching-substring>
          </xsl:analyze-string>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
          <xsl:value-of select="."/>
        </xsl:non-matching-substring>
      </xsl:analyze-string>
    </xsl:value-of>
  </xsl:function>

  <!-- call like this -->

  <xsl:value-of select="mf:titlecase($sentence, ('and', 'or', 'MATM'))"/>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
bonekrusher (September 16th, 2010)
 
Old September 16th, 2010, 10:04 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Sam/Martin

Thanks for the quick reply!

Both solutions worked.

Thanks again

Regards,





Similar Threads
Thread Thread Starter Forum Replies Last Post
Report Title shanib Crystal Reports 1 October 7th, 2005 02:10 PM
Raising the case of a string Dave Brown Beginning PHP 3 April 15th, 2005 02:08 AM
search string either Upper case or lower case rylemer Beginning VB 6 3 March 24th, 2004 04:23 PM
Check Case in a Case-Insensitive DB nbryson SQL Language 1 January 23rd, 2004 07:36 AM
Name or Title charlie3 VBScript 2 July 8th, 2003 05:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.