ct5845
ct5845

Reputation: 1588

XSLT Date Comparisons

<xsl:variable name="date1" select="2011-10-05"/>
<xsl:variable name="date2" select="2011-10-05"/>
<xsl:variable name="date3" select="2011-10-06"/>

<xsl:if test="$date2 = $date1 or $date2 &lt; $date1">
  ..do something
</xsl:if>

<xsl:if test="$date3 = $date1 or $date3 &gt; $date1">
 .. do something
</xsl:if>

Both should evaluate true, but the second if doesn't. For the life of me I can't comprehended why!

In the actual transform the dates themselves are being drawn from an XML document but debugging through VS2010 i can see the values are as above.

Must be something fairly fundamental i'm doing wrong - any help would be brilliant!

Upvotes: 1

Views: 1949

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

I tried this in Oxygen/XML... select="2011-10-05 is being interpreted as an arithmetic expression, giving the value 1996 (2011 minus 10 minus 5) and "2011-10-06" is intrepreted as 1995.

What you want is

<xsl:variable name="date1" select="'2011-10-05'"/>
<xsl:variable name="date2" select="'2011-10-05'"/>
<xsl:variable name="date3" select="'2011-10-06'"/>

Note the extra single quotes.

From the XSLT 1.0 Specification:

If the variable-binding element has a select attribute, then the value of the attribute must be an expression and the value of the variable is the object that results from evaluating the expression.

Upvotes: 2

Related Questions