Tecnologías XML / XSLT / XSLT Kochbuch / Fechas y horas / Obtener los nombres de los días y los meses
(Extracto de «XSLT Kochbuch" de Sal Mangano)
Problema
Desea convertir los valores numéricos de los días y los meses en valores simbólicos.
Solución
XSLT 1.0
Si Internacionalización no es importante para su aplicación, basta con el siguiente código sencillo:
<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 ofrece la función format-date( ), que resulta útil para esta receta y muchas otras tareas. Según su implementación, también puede admitir la internacionalización:
<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>El segundo parámetro de format-date es una cadena de formato que encierra códigos de formato entre corchetes (por ejemplo, [D01]). Existe una gran variedad de opciones de formato. Para obtener más información, consulte Formato de fechas y horas.
Discusión
XSLT 1.0
Estas plantillas son adecuadas si sus aplicaciones nunca se utilizan fuera del mundo anglófono. Sin embargo, para mejorar su portabilidad, considere un enfoque basado en tablas:
<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
Las funciones format-date( ), format-time( ) y format-dateTime( ) son muy versátiles. Además de las capacidades mostradas en estas recetas, pueden dar formato de muchas maneras a todos los componentes de las fechas y las horas. También permiten especificar códigos de país, calendario e idioma, lo que posibilita la internacionalización dentro de los límites de su implementación.
Véase también
Para consultar la especificación completa de las funciones de formato de fechas de XSLT 2.0, véase la receta Dar formato a fechas y horas.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
Puede imprimir la versión en línea para uso privado.
En los demás aspectos, este capítulo del libro "XSLT Kochbuch" está sujeto a las mismas condiciones que la edición impresa: la obra y todas sus partes están protegidas por derechos de autor. Se reservan todos los derechos, incluidos los de reproducción, traducción, microfilmación y almacenamiento y procesamiento en sistemas electrónicos.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de