Johnathan Au
Johnathan Au

Reputation: 5362

How can I capitalize only the first letter given that the entire word is capitalized, in XSLT 1.0?

Given that I have a set of months in capital letters:

<months>
    <month name="JAN"/>
    <month name="FEB"/>
    <month name="MAR"/>
    <month name="APR"/>
    <month name="MAY"/>
    <month name="JUN"/>
    <month name="JUL"/>
    <month name="AUG"/>
    <month name="SEP"/>
    <month name="OCT"/>
    <month name="NOV"/>
    <month name="DEC"/>
</months>

How can I capitalize only the first letter?

This is my solution right now but I am using CSS to capitalize it. I want to see how it can be done in pure XSLT 1.0

<xsl:template match="months">
    <xsl:variable name="month" select="month/@name"/>       
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
    <xsl:variable name="monthFormatted"><xsl:value-of select="translate($month, $uppercase, $smallcase)" /></xsl:variable>  
</xsl:template>

/* CSS */
.months {
    text-transform: capitalize;
}

Upvotes: 0

Views: 4730

Answers (3)

Suresh
Suresh

Reputation: 59

Well solution

concat(translate(substring($Name, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring($Name,2,string-length($Name)-1))

Upvotes: 1

Yash Vyas
Yash Vyas

Reputation: 33

Provide attribute : use-attribute-sets

<xsl:template match="months" xsl:use-attribute-sets="style" >

Add style :

<xsl:attribute-set name="style">
         <xsl:attribute name="text-transform">capitalize</xsl:attribute>
</xsl:attribute-set>

Upvotes: 3

Martin Honnen
Martin Honnen

Reputation: 167571

Well with

<xsl:param name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:param name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>

<xsl:template match="month">
  <xsl:value-of select="concat(substring(@name, 1, 1), translate(substring(@name, 2), $uppercase, $lowercase))"/>
</xsl:template>

you should get e.g Jan, Feb, ....

Upvotes: 2

Related Questions