Recipe4.3.Getting Names for Days and Months


Recipe 4.3. Getting Names for Days and Months

Problem

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

Solution

XSLT 1.0

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

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

XSLT 2.0 has the format-date() function, which solves this recipe and much more. Depending on your implementation, it may also handle internationalization:

<xsl:function name="ckbk:get-day-of-the-week-name" as="xs:string">   <xsl:param name="day-of-the-week" as="xs:integer"/>   <!-- Any old Sunday will do as the base. Here I arbitrarily picked         Sunday, Aug 14 2005 as my base date because it happens to be the         day I am writing this-->   <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"/>   <!-- Here we use date+duration math rather than string manipulation to get        the date we need for format-date -->    <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 to format-date is a picture string that surrounds formatting codes in square brackets (e.g., [D01]). There is a rich variety of formatting options available. See http://www.w3.org/TR/xslt20/#function-format-date for more details.

Discussion

XSLT 1.0

These templates are just fine if your application will never be used outside of the English-speaking world. However, you might consider using a table-driven approach for added portability:

<xsl:stylesheet   version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xmlns:date="http://www.ora.com/XSLTCookbook/NS/dates">     <!-- United States : 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"/>     <!-- Germany : 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"/> <!-- You get the idea ... -->     <!-- Store element in variable for easy access --> <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>

XSLT 2.0

The format-date( ), format-time(), and format-dateTime( ) functions are quite rich. In addition to the capabilities demonstrated by these recipes, it can format all components of dates and times in multiple ways. They also allow country, calendar, and language codes to be specified, so internationalization is free (subject to the limits of your implementation).

See Also

The full specification of the XSLT 2.0 date formatting functions can be found in Recipe 4.10.




XSLT Cookbook
XSLT Cookbook: Solutions and Examples for XML and XSLT Developers, 2nd Edition
ISBN: 0596009747
EAN: 2147483647
Year: 2003
Pages: 208
Authors: Sal Mangano

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net