test.xml
<...>
<date serverFormat="MMDDYYYY" clientFormat="MM/DD/YYYY">04092001<date>
<date serverFormat="MMDDYYYY" clientFormat="MM-DD-YYYY">04092001<date>
<date serverFormat="MMDDYYYY" clientFormat="YYYY/DD/MM">04092001<date>
<date serverFormat="YYYY/MM/DD"
clientFormat="YYY,MM,DD">04092001<date>
<...>
my expected output would be..
04/09/2001
04-09-2001
2001/09/04
2001,04,09
so on and so on...
Any methods or hints to approach this problem without hard-coding?
Thank you,
Ming
> test.xml
> <...>
> <date serverFormat="MMDDYYYY" clientFormat="MM/DD/YYYY">04092001<date>
> <date serverFormat="MMDDYYYY" clientFormat="MM-DD-YYYY">04092001<date>
> <date serverFormat="MMDDYYYY" clientFormat="YYYY/DD/MM">04092001<date>
> <date serverFormat="YYYY/MM/DD"
> clientFormat="YYY,MM,DD">04092001<date>
> <...>
The XSLT FAQ Dates section at
http://www.dpawson.co.uk/xsl/sect2/dates.html has a nice example (#2)
of how to get different formats of a date of the form YYYYMMDD. The
idea is to write a named template for each result format You can then
call them when needed with a xsl:call-template instruction, e.g.:
<xsl:call-template name="short_date"
<xsl:with-parm name="date" select="20010101"/>
</xsl:call-template>
Due to the different source format of your date data, you'd have to
build your own set of templates. To add some modularity, put them in a
separate stylesheet file, and include in the main stylesheet with
xsl:include.
Hope this helps,
Tuija
--
Tuija Sonkkila
HUT CC
I would like a clean solution that doesn't deals with writing endless
of stylesheet templates. Is there a functions that handles date
format similar to number-format() in XSLT Function?
Thanks all,
Ming.
mfang329> I would like a clean solution that doesn't deals with writing
mfang329> endless of stylesheet templates. Is there a functions that
mfang329> handles date format similar to number-format() in XSLT Function?
I have not tried, but XSLT Standard Library (http://xsltsl.sourceforge.net/)
has a module for date/time string formatting and parsing.
--
Arto V. Viitanen a...@cs.uta.fi
University of Tampere, Department of Computer and Information Sciences
Tampere, Finland http://www.cs.uta.fi/~av/
That was a good pointer, thank you! I did some homework and tested the
dt:format-date-time template against Ming's original examples. Here
are the results of the home lab :)
test.xml:
<dates>
<date serverFormat="MMDDYYYY" clientFormat="MM/DD/YYYY">04092001</date>
<date serverFormat="MMDDYYYY" clientFormat="MM-DD-YYYY">04092001</date>
<date serverFormat="MMDDYYYY" clientFormat="YYYY/DD/MM">04092001</date>
<date serverFormat="YYYY/MM/DD" clientFormat="YYYY,MM,DD">04092001</date>
</dates>
test.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:dt="http://xsltsl.org/date-time">
<xsl:output method="text"/>
<xsl:import href="stdlib.xsl"/>
<xsl:template match="dates/date">
<xsl:call-template name="whatFormat">
<xsl:with-param name="date" select="@clientFormat"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="whatFormat">
<xsl:param name="date"/>
<xsl:variable name="dateForm">
<xsl:choose>
<xsl:when test="$date='MM/DD/YYYY'">
<xsl:value-of select="'%m/%d/%Y'"/>
</xsl:when>
<xsl:when test="$date='MM-DD-YYYY'">
<xsl:value-of select="'%m-%d-%Y'"/>
</xsl:when>
<xsl:when test="$date='YYYY/DD/MM'">
<xsl:value-of select="'%Y/%d/%m'"/>
</xsl:when>
<xsl:when test="$date='YYYY,MM,DD'">
<xsl:value-of select="'%Y,%m,%d'"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:call-template name="dt:format-date-time">
<xsl:with-param name="year" select="substring(.,5,4)"/>
<xsl:with-param name="month" select="substring(.,1,2)"/>
<xsl:with-param name="day" select="substring(.,3,2)"/>
<xsl:with-param name="format" select="$dateForm"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
test.txt:
04/09/2001
04-09-2001
2001/09/04
2001,04,09
Tuija
PS. After the first test run, the result was one extra '0' in both day and
month. The reason was a xsl:if test in the template (here the one
testing month):
<!-- Month as decimal number (01 - 12) -->
<xsl:when test="$code='m'">
<xsl:if test="$month < 10">0</xsl:if>
<xsl:value-of select="$month"/>
</xsl:when>
> <!-- Month as decimal number (01 - 12) -->
> <xsl:when test="$code='m'">
> <xsl:if test="$month < 10">0</xsl:if>
> <xsl:value-of select="$month"/>
> </xsl:when>
Sorry, forgot to mention that I'm (still) using XT, if that's relevant
here, I'm not sure. Anyway, perhaps the test'd rather be:
<xsl:if test="string-length($month) < 2">0</xsl:if>
But this is something for the xsltsl project really, so I'm not taking
*your* time any longer... bye.
Tuija
Just one more thing: this has been already fixed (but not the way I
figured). If you are interested, see the diff listing at
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/xsltsl/xsltsl/date-time.xsl.diff?r1=1.5&r2=1.6