O'Reilly logo

XSLT Cookbook by Sal Mangano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting Names for Days and Months

Problem

You want to convert from numerical values for days and months to symbolic values.

Solution

If internationalization is not important to your application, then the following simple code will do:

 <xsl:template name="date:get-day-of-the-week-name"> <xsl:param name="day-of-the-week"/> <xsl:choose> <xsl:when test="$day-of-the-week = 0">Sunday</xsl:when> <xsl:when test="$day-of-the-week = 1">Monday</xsl:when> <xsl:when test="$day-of-the-week = 2">Tuesday</xsl:when> <xsl:when test="$day-of-the-week = 3">Wednesday</xsl:when> <xsl:when test="$day-of-the-week = 4">Thursday</xsl:when> <xsl:when test="$day-of-the-week = 5">Friday</xsl:when> <xsl:when test="$day-of-the-week = 6">Saturday</xsl:when> <xsl:otherwise> error: <xsl:value-of select="$day-of-the-week"/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="date:get-day-of-the-week-abbreviation"> <xsl:param name="day-of-the-week"/> <xsl:choose> <xsl:when test="$day-of-the-week = 0">Sun</xsl:when> <xsl:when test="$day-of-the-week = 1">Mon</xsl:when> <xsl:when test="$day-of-the-week = 2">Tue</xsl:when> <xsl:when test="$day-of-the-week = 3">Wed</xsl:when> <xsl:when test="$day-of-the-week = 4">Thu</xsl:when> <xsl:when test="$day-of-the-week = 5">Fri</xsl:when> <xsl:when test="$day-of-the-week = 6">Sat</xsl:when> <xsl:otherwise> error: <xsl:value-of select="$day-of-the-week"/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="date:get-month-name"> <xsl:param name="month"/> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required