Re: [xsl] Capitalizing only the first letter of each word

Subject: Re: [xsl] Capitalizing only the first letter of each word
From: Ingo Schildmann <Ingo.Schildmann@xxxxxxxxxxxxx>
Date: Thu, 21 Mar 2002 15:22:07 +0100
On Thursday 21 March 2002 02:42, you wrote:
> I am getting back my data in ALL-CAPS.  Is there any way to display the
> first letter of each word in uppercase, but the rest of each word in
> lowercase?
>
> example XML:
>
> <Tg Nm="Name">JOHN LEWIS</Fld>
> <Tg Nm="Street">8780 MAIN ST</Fld>
> <Tg Nm="City">COLORADO SPRINGS</Fld>
> <Tg Nm="State">CO</Fld>
>
> I want to display:
>
> John Lewis
> 8780 Main St
> Colorado Springs
> CO
>

Here is a sketched (means not tested) XSLT solution,
with apologies to Scotts(McD..), Irishs(O'H..) and people from IJsselstein:-).
Imho you shouldn't change the strings because you risk to falsify the data,
the problem is the loss of cases in the process before.

Call template caseUp with a string you want to convert.
Every char that's not at the beginning or after a space is decapitalized.

<xsl:template name="caseDown">
    <xsl:param name="data"/>
    <xsl:if test="$data">
       <xsl:choose>
        <xsl:when test="starts-with($data,' ')"> 
            <xsl:text> </xsl:text> 
            <xsl:call-template name="caseUp">
               <xsl:with-param name="data" select="substring($data,2)"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="translate(substring($data,1,1),
                                  'ABCDE....XYZ','abcde...xyz')"/>
            <!-- put all the chars you want to change 
                 into the last two strings -->        
            <xsl:call-template name="caseDown">
               <xsl:with-param name="data" select="substring($data,2)"/>
            </xsl:call-template>
        </xsl:otherwise>
       </xsl:choose>
    </xsl:if>  
</xsl:template>

<xsl:template name="caseUp">
   <xsl:param name="data"/>
   <xsl:if test="$data">
	<xsl:value-of select="substring($data,1,1)"/>
        <xsl:call-template name="caseDown"> 
            <xsl:with-param name="data" select="substring($data,2)"/>
        </xsl:call-template>
   </xsl:if>
</xsl:template>

   
-- 
Ingo Schildmann                       
Development
WiredMinds Informationssysteme GmbH
http://www.WiredMinds.de


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread