Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to process various date format?

106 views
Skip to first unread message

Ming

unread,
Nov 6, 2001, 12:21:12 PM11/6/01
to
I have a dilemma with how to process various date format. The problem
is I would not want to hard code the stylesheet to do only a certain
type of date format, but I would rather want to code the stylesheet to
accept multiple date format. For example..

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

Tuija Sonkkila

unread,
Nov 6, 2001, 2:23:55 PM11/6/01
to
Ming <mfan...@yahoo.com> wrote:
> I have a dilemma with how to process various date format. The problem
> is I would not want to hard code the stylesheet to do only a certain
> type of date format, but I would rather want to code the stylesheet to
> accept multiple date format. For example..

> 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

Ming

unread,
Nov 6, 2001, 5:23:42 PM11/6/01
to
What you have mentioned above is fine, but there comes a problem when
the stylesheet developer have to write endless combinations of date
format templates and that is not the idea I want to approach my
problem.

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.

Arto Viitanen

unread,
Nov 7, 2001, 1:23:01 AM11/7/01
to
>>>>> "mfang329" == mfang329 <mfan...@yahoo.com> writes:

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/

Tuija Sonkkila

unread,
Nov 7, 2001, 5:17:43 AM11/7/01
to
Arto Viitanen <a...@cs.uta.fi> wrote:
> I have not tried, but XSLT Standard Library (http://xsltsl.sourceforge.net/)
> has a module for date/time string formatting and parsing.

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 &lt; 10">0</xsl:if>
<xsl:value-of select="$month"/>
</xsl:when>

Tuija Sonkkila

unread,
Nov 7, 2001, 5:38:48 AM11/7/01
to
Tuija Sonkkila <sonk...@cc.hut.fi> wrote:
> 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 &lt; 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) &lt; 2">0</xsl:if>

But this is something for the xsltsl project really, so I'm not taking
*your* time any longer... bye.

Tuija

Tuija Sonkkila

unread,
Nov 7, 2001, 8:10:59 AM11/7/01
to
Tuija Sonkkila <sonk...@cc.hut.fi> wrote:
> Anyway, perhaps the test'd rather be: <xsl:if
> test="string-length($month) &lt; 2">0</xsl:if>

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

0 new messages