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

StackOverflow https://stackoverflow.com/questions/19001870

  •  29-06-2022
  •  | 
  •  

Question

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;
}
Was it helpful?

Solution 2

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, ....

OTHER TIPS

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>

Well solution

concat(translate(substring($Name, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring($Name,2,string-length($Name)-1))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top