Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 16th, 2010, 08:56 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
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.
Reply With Quote
  #2 (permalink)  
Old September 16th, 2010, 09:15 AM
samjudson's Avatar
Friend of Wrox
Points: 8,448, Level: 39
Points: 8,448, Level: 39 Points: 8,448, Level: 39 Points: 8,448, Level: 39
Activity: 37%
Activity: 37% Activity: 37% Activity: 37%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,088
Thanks: 1
Thanked 181 Times in 180 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?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
bonekrusher (September 16th, 2010)
  #3 (permalink)  
Old September 16th, 2010, 09:19 AM
Friend of Wrox
Points: 6,438, Level: 34
Points: 6,438, Level: 34 Points: 6,438, Level: 34 Points: 6,438, Level: 34
Activity: 25%
Activity: 25% Activity: 25% Activity: 25%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,196
Thanks: 0
Thanked 236 Times in 235 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
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
bonekrusher (September 16th, 2010)
  #4 (permalink)  
Old September 16th, 2010, 10:04 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
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,
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 03:23 PM
Check Case in a Case-Insensitive DB nbryson SQL Language 1 January 23rd, 2004 06:36 AM
Name or Title charlie3 VBScript 2 July 8th, 2003 05:00 PM



All times are GMT -4. The time now is 02:53 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.