XML technologies / XSLT / XSLT Kochbuch / Dates and times / Obtaining day and month names
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
You want to convert numeric day and month values into symbolic values.
Solution
XSLT 1.0
If Internationalization does not matter for your application, the following simple code suffices:
<xsl:template name="ckbk: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="ckbk: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="ckbk:get-month-name">
<xsl:param name="month"/>
<xsl:choose>
<xsl:when test="$month = 1">January</xsl:when>
<xsl:when test="$month = 2">February</xsl:when>
<xsl:when test="$month = 3">March</xsl:when>
<xsl:when test="$month = 4">April</xsl:when>
<xsl:when test="$month = 5">May</xsl:when>
<xsl:when test="$month = 6">June</xsl:when>
<xsl:when test="$month = 7">July</xsl:when>
<xsl:when test="$month = 8">August</xsl:when>
<xsl:when test="$month = 9">September</xsl:when>
<xsl:when test="$month = 10">October</xsl:when>
<xsl:when test="$month = 11">November</xsl:when>
<xsl:when test="$month = 12">December</xsl:when>
<xsl:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ckbk:get-month-abbreviation">
<xsl:param name="month"/>
<xsl:choose>
<xsl:when test="$month = 1">Jan</xsl:when>
<xsl:when test="$month = 2">Feb</xsl:when>
<xsl:when test="$month = 3">Mar</xsl:when>
<xsl:when test="$month = 4">Apr</xsl:when>
<xsl:when test="$month = 5">May</xsl:when>
<xsl:when test="$month = 6">Jun</xsl:when>
<xsl:when test="$month = 7">Jul</xsl:when>
<xsl:when test="$month = 8">Aug</xsl:when>
<xsl:when test="$month = 9">Sep</xsl:when>
<xsl:when test="$month = 10">Oct</xsl:when>
<xsl:when test="$month = 11">Nov</xsl:when>
<xsl:when test="$month = 12">Dec</xsl:when>
<xsl:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise>
</xsl:choose>
</xsl:template>XSLT 2
XSLT 2.0 provides the function format-date( ), which is useful for this recipe and much more. Depending on your implementation, it may also support internationalization:
<xsl:function name="ckbk:get-day-of-the-week-name" as="xs:string">
<xsl:param name="day-of-the-week" as="xs:integer"/>
<!-- Irgendein alter Sonntag ist als Basis ausreichend. Ich habe mich hier zufällig für Sonntag, den 14. August entschieden, da dies der Tag ist, an dem ich diese Zeilen geschrieben habe-->
<xsl:variable name="date" select="concat('2005-08-', string(14 + $day-of-the-week))" as="xs:date"/>
<xsl:sequence select="format-date($date,"[F]"/>
</xsl:function>
<xsl:function name="ckbk:get-day-of-the-week-name-abbr" as="xs:string">
<xsl:param name="day-of-week" as="xs:integer"/>
<xsl:variable name="date" select="concat('2005-08-', string(14 + $day-of-the-week))" as="xs:date"/>
<xsl:sequence select="format-date($date, '[FNn,3-3]')"/>
</xsl:function>
<xsl:function name="ckbk:get-month-name" as="xs:string">
<xsl:param name="month" as="xs:integer"/>
<xsl:variable name="jan01" select="xs:date('2005-01-01')" as="xs:date"/>
<!-- Hier verwenden wir eine Datums- und Zeitdauerberechnung anstelle einer Stringmanipulation, um das Datum zu erhalten, das wir für format-date brauchen -->
<xsl:sequence select="format-date($jan01 + xdt:yearMonthDuration(concat('P',$month - 1,'M')), '[MNn]')"/>
</xsl:function>
<xsl:function name="ckbk:get-month-name-abbr" as="xs:string">
<xsl:param name="month" as="xs:integer"/>
<xsl:variable name="jan01" select="xs:date('2005-01-01')" as="xs:date"/>
<xsl:sequence select="format-date($jan01 + xdt:yearMonthDuration(concat('P',$month - 1,'M')), '[MNn,3-3]')"/>
</xsl:function>The second parameter of format-date is a picture string enclosing formatting codes in square brackets (for example, [D01]). A wide range of formatting options is available. For details, see Formatting Dates and Times.
Discussion
XSLT 1.0
These templates are adequate if your applications are never used outside the English-speaking world. For greater portability, however, consider a table-based approach:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://www.ora.com/XSLTCookbook/NS/dates">
<!-- Vereinigte Staaten von Amerika : us -->
<ckbk:month country="us" m="1" name="January" abbrev="Jan" />
<ckbk:month country="us" m="2" name="February" abbrev="Feb"/>
<ckbk:month country="us" m="3" name="March" abbrev="Mar"/>
<ckbk:month country="us" m="4" name="April" abbrev="Apr"/>
<ckbk:month country="us" m="5" name="May" abbrev="May"/>
<ckbk:month country="us" m="6" name="June" abbrev="Jun"/>
<ckbk:month country="us" m="7" name="July" abbrev="Jul"/>
<ckbk:month country="us" m="8" name="August" abbrev="Aug"/>
<ckbk:month country="us" m="9" name="September" abbrev="Sep"/>
<ckbk:month country="us" m="10" name="October" abbrev="Oct"/>
<ckbk:month country="us" m="11" name="November" abbrev="Nov"/>
<ckbk:month country="us" m="12" name="December" abbrev="Dec"/>
<!-- Deutschland : de -->
<ckbk:month country="de" m="1" name="Januar" abbrev="Jan"/>
<ckbk:month country="de" m="2" name="Februar" abbrev="Feb"/>
<ckbk:month country="de" m="3" name="März" abbrev="Mär"/>
<ckbk:month country="de" m="4" name="April" abbrev="Apr"/>
<ckbk:month country="de" m="5" name="Mai" abbrev="Mai"/>
<ckbk:month country="de" m="6" name="Juni" abbrev="Jun"/>
<ckbk:month country="de" m="7" name="Juli" abbrev="Jul"/>
<ckbk:month country="de" m="8" name="August" abbrev="Aug"/>
<ckbk:month country="de" m="9" name="September" abbrev="Sep"/>
<ckbk:month country="de" m="10" name="Oktober" abbrev="Okt"/>
<ckbk:month country="de" m="11" name="November" abbrev="Nov"/>
<ckbk:month country="de" m="12" name="Dezember" abbrev="Dez"/>
<!-- Alles klar? ... -->
<!-- Speichern des Elements für einen einfachen Zugriff in Variablen -->
<xsl:variable name="ckbk:months" select="document('')/*/ckbk:month"/>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://www.ora.com/XSLTCookbook/dates">
<xsl:include href="date-conversion.xsl"/>
<xsl:template name="ckbk:get-month-name">
<xsl:param name="month"/>
<xsl:param name="country" select=" 'us' "/>
<xsl:value-of select="$ckbk:months[@country=$country and @m=$month]/@name"/>
</xsl:template>
</xsl:stylesheet>XSLT 2.0
The functions format-date( ), format-time( ) and format-dateTime( ) are quite versatile. In addition to the capabilities demonstrated in these recipes, they can format every component of dates and times in many ways. They also accept country, calendar and language codes, enabling internationalization subject to the limitations of your implementation.
See also
For the complete specification of the XSLT 2.0 date-formatting functions, see the recipe Formatting dates and times.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de