[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Processing a Sequence of String Tokens in XSLT 2

Subject: Re: Processing a Sequence of String Tokens in XSLT 2
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Wed, 6 Jun 2007 20:52:10 +0100
Re:  Processing a Sequence of String Tokens in XSLT 2
On 6/6/07, Eliot Kimber <ekimber@xxxxxxxxxxxxxxxxxxx> wrote:
I suspect this is an FAQ, and if so, feel free to point me to existing
discussion, but I'm curious as to what the most efficient/elegant way to
process a sequence of string tokens is.

In my specific case, I need to init-cap all the words in a string (I
don't need any sophistication like special case for conjunctions or

Here's the XSLT 2 function I came up with:

     <!-- Normalizes the case of titles based on the FASB-defined rules
for title case -->
     <xsl:variable name="titleTokens"
select="tokenize(string($titleElem), ' ')"/>
     <xsl:variable name="resultString">
       <xsl:for-each select="$titleTokens">
         <xsl:sequence select="concat(upper-case(substring(., 1,1)),
substring(., 2))"/>
     <xsl:sequence select="$resultString"/>

Which seems reasonably compact and understandable but I suspect that I'm
not doing things as cleverly or as "correctly" as I could.

Is there a better way to have written this function?

I would add the "as" for the the param, return value and the variables... and you may need a <xsl:value-of/> in there to get a single string from a sequence of strings as the return value. I think also you may be relying on the default separator used between items in the sequence to the single space back that gets lost during the tokenization.

With all that in mind, here's a version with the modifications I've mentioned:

<xsl:function  name="func:normalizeTitleContent" as="xs:string">
<xsl:param name="titleStr" as="xs:string"/>
 <xsl:value-of separator=" ">
  <xsl:for-each select="tokenize($titleStr, ' ')">
   <xsl:sequence select="concat(upper-case(substring(., 1,1)),
substring(., 2))"/>

which is the same as:

<xsl:function  name="func:normalizeTitleContent" as="xs:string">
<xsl:param name="titleStr" as="xs:string"/>
<xsl:sequence select="string-join(for $word in tokenize($titleStr, ' ') return
concat(upper-case(substring($word, 1,1)), substring($word, 2)), ' ')"/>


Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.