Reputation: 5362
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
Reputation: 59
Well solution
concat(translate(substring($Name, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring($Name,2,string-length($Name)-1))
Upvotes: 1
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
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