Discussion:
date comparison
(too old to reply)
v***@gmail.com
2007-07-08 00:12:30 UTC
Permalink
How do you do a date comparison in xslt?

if Ihave something like:

<xsl:value-of select="format-number(sum($something[ xyz ... ]

where xyz is checking the value of some variable like "valid=y", and I
want to extend that check to say "if valid = y and date > 1 Jan 2007"
how do I do it?

Should something like this work?

date &gt; '01-Jan-2007' ?

date is coming from a sybase table.

Many thanks,
Martin Honnen
2007-07-08 11:37:43 UTC
Permalink
Post by v***@gmail.com
How do you do a date comparison in xslt?
XSLT 1.0 does not know about dates. XSLT 2.0 has support for the XML
schema xs:date data type so there you can do e.g. <xsl:value-of select="xs:date('2007-01-01') &gt; xs:date('2007-07-08')"/>

With XSLT 1.0 your only chance is to convert the dates to numbers and
compare those e.g. <xsl:value-of select="number(translate('2007-01-01', '-', '')) &gt;
number(translate('2007-07-08', '-', ''))"/>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Wes
2007-11-22 05:58:00 UTC
Permalink
Building on this... I'm trying to compare two dateTimes in an xsl:when test=
statement.

The logic I need is to compare as follows:
xsl:when test="dateTime1 <= dateTime2-(1 hour)"

TIA for any help you can offer.
Post by Martin Honnen
Post by v***@gmail.com
How do you do a date comparison in xslt?
XSLT 1.0 does not know about dates. XSLT 2.0 has support for the XML
schema xs:date data type so there you can do e.g.
<xsl:value-of select="xs:date('2007-01-01') > xs:date('2007-07-08')"/>
With XSLT 1.0 your only chance is to convert the dates to numbers and
compare those e.g.
<xsl:value-of select="number(translate('2007-01-01', '-', '')) >
number(translate('2007-07-08', '-', ''))"/>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Martin Honnen
2007-11-22 13:14:32 UTC
Permalink
Post by Wes
Building on this... I'm trying to compare two dateTimes in an xsl:when test=
statement.
xsl:when test="dateTime1 <= dateTime2-(1 hour)"
Are you using XSLT 2.0 or XSLT 1.0?
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Wes
2007-11-22 14:10:00 UTC
Permalink
2.0
Post by Martin Honnen
Post by Wes
Building on this... I'm trying to compare two dateTimes in an xsl:when test=
statement.
xsl:when test="dateTime1 <= dateTime2-(1 hour)"
Are you using XSLT 2.0 or XSLT 1.0?
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Martin Honnen
2007-11-22 14:40:45 UTC
Permalink
2.0
XPath 2.0 supports comparison on xs:dateTime values and supports adding
and substracting durations to xs:dateTime values e.g.

($d2 - xs:dayTimeDuration('P0DT1H0M0S')

substracts 0 days, 1 hour, 0 minutes and 0 seconds from the xs:dateTime
$d2 so your original request can be written as

xsl:when test="$dateTime1 &lt;= ($dateTime2--
xs:dayTimeDuration('P0DT1H0M0S'))"
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Wes
2007-11-22 14:42:01 UTC
Permalink
Scratch that last reply, it's 1.0. I'm in WSS 3.0 (Sharepoint)

Thanks,
2.0
Post by Martin Honnen
Post by Wes
Building on this... I'm trying to compare two dateTimes in an xsl:when test=
statement.
xsl:when test="dateTime1 <= dateTime2-(1 hour)"
Are you using XSLT 2.0 or XSLT 1.0?
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Martin Honnen
2007-11-22 16:00:49 UTC
Permalink
Post by Wes
Scratch that last reply, it's 1.0. I'm in WSS 3.0 (Sharepoint)
XSLT/XPath 1.0 does not have support for XSD schema types like
xs:dateTime or durations. Does WSS 3.0 allow you to to use msxsl:script
in stylesheet to implement extension functions? That way you could use
C# or VB to write extension functions and make use of .NET DateTime.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Wes
2007-11-26 20:33:01 UTC
Permalink
hmmm... I posted a response that is not showing up... so here it is again.

Thanks Martin for your help on this. Building on your 1.0 advice I was able
to get the result I needed with a rather ugly statement like this:

<xsl:variable name="HoursBefore">1</xsl:variable>

<xsl:when
test="number(concat(normalize-space(ddwrt:FormatDateTime(substring(DateTime1,1,10),1033,'yyyyMMdd')),'.',number(substring(DateTime1,12,2))-number($HoursBefore),substring(DateTime1,15,2)))<=number(concat(normalize-space(ddwrt:FormatDateTime(ddwrt:TodayIso(),1033,'yyyyMMdd')),'.',substring(ddwrt:TodayIso(),12,2),substring(ddwrt:TodayIso(),15,2)))">

It isn't pretty, but it seems to work.

Thanks again,

Wes
Post by Martin Honnen
Post by Wes
Scratch that last reply, it's 1.0. I'm in WSS 3.0 (Sharepoint)
XSLT/XPath 1.0 does not have support for XSD schema types like
xs:dateTime or durations. Does WSS 3.0 allow you to to use msxsl:script
in stylesheet to implement extension functions? That way you could use
C# or VB to write extension functions and make use of .NET DateTime.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Loading...