This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: compare date value with 2 other ones
- To: Kristof Bernaert <kbernaert at 1eEurope dot be>
- Subject: Re: [xsl] compare date value with 2 other ones
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Thu, 5 Apr 2001 11:34:52 +0100
- CC: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Organization: Jeni Tennison Consulting Ltd
- References: <4B67D93B3C8CD311AE81009027D5E55EA014E2@DAVINCI>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Kristof,
> How have i to handle the date ? to compare correctly the day, month
> and year ?
It's a little tricky - XSLT doesn't have any built in support for
comparison of dates, so you have to change them into something that it
can compare, namely numbers.
With the formats you're using (assuming they're English rather than
American, i.e. that the first number is the day), you can get the year
of a date with:
substring($date, 7)
the month with:
substring($date, 4, 2)
and the day with:
substring($date, 1, 2)
Now, you can either do the comparisons by comparing the year, then
comparing the month, then comparing the day, or by constructing a
single string that holds them all in the order year, month, day. The
latter is probably easier.
Either way, you'll find it convenient to assign the current date to a
variable:
<xsl:variable name="current_date"
select="document('current_date.xml')/current_date" />
I'd then construct the comparable date - one that looks like 20010405,
for example, from that variable:
<xsl:variable name="compare_date"
select="concat(substring($current_date, 7),
substring($current_date, 4, 2),
substring($current_date, 1, 2))" />
Then, within the selection, you need to likewise construct dates in
the YYYYMMDD format from the @startdate and @enddate attributes:
<xsl:for-each select="news/block
[(concat(substring(@startdate, 7),
substring(@startdate, 4, 2),
substring(@startdate, 1, 2)) =>
$compare_date) and
(concat(substring(@enddate, 7),
substring(@enddate, 4, 2),
substring(@enddate, 1, 2)) <=
$compare_date)]">
<xsl:apply-templates/>
</xsl:for-each>
BTW, you probably just want:
<xsl:apply-templates
select="news/block
[(concat(substring(@startdate, 7),
substring(@startdate, 4, 2),
substring(@startdate, 1, 2)) =>
$compare_date) and
(concat(substring(@enddate, 7),
substring(@enddate, 4, 2),
substring(@enddate, 1, 2)) <=
$compare_date)]" />
Rather than wrapping an xsl:for-each around an xsl:apply-templates,
unless you're doing something else within the xsl:for-each that you
haven't shown.
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list