Vivian River
Vivian River

Reputation: 32390

How to compare two date variables in XSLT?

I've written some simple XSLT below. The long and the short is that I want to do two things.

First, I want to print whether or not this is a new order. For my purposes, an order is new if the OrderCreateDate and OrderEditDate fields are the same.

Second, I want to print the two dates (to check my work; formatting isn't important, yet.)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"                
>              
<xsl:output method="text" indent="yes"/>

<xsl:template match="/ArrayOfDP_RunDatesAll">
  <xsl:variable name='OrderCreateDate' select='DP_RunDatesAll[1]/OrderCreateDate'/>
  <xsl:variable name='OrderEditDate' select='DP_RunDatesAll[1]/OrderEditDate'/>
  <xsl:choose>
    <xsl:when test='OrderCreateDate != OrderEditDate'>
      Existing Order!
    </xsl:when>
    <xsl:otherwise>      
      New Order!
      OrderCreateDate <xsl:value-of select='$OrderCreateDate' />
      OrderEditDate   <xsl:value-of select='$OrderEditDate' />
    </xsl:otherwise>
  </xsl:choose>       
</xsl:template>  
</xsl:stylesheet>

I would expect that with the above XSLT, when a record is processed with two separate dates, the output should be Existing Order!.

However, I see the following output.

  New Order!
  OrderCreateDate 2013-08-16T10:27:39
  OrderEditDate   2013-08-17T17:19:43.6    

By the rules I described above, it should have printed Existing Order! (because the two dates don't match). What have I done to get the wrong result here?

Upvotes: 2

Views: 4849

Answers (3)

Manjunath Patelappa
Manjunath Patelappa

Reputation: 77

best way of doing this is:

doing without string function is also correct but some times it doesn't work

<xsl:when test='string($OrderCreateDate) != string($OrderEditDate)'>

Upvotes: -1

Dijkgraaf
Dijkgraaf

Reputation: 11527

You need to have your test use the variables correctly (have the $ signs).

<xsl:when test='$OrderCreateDate != $OrderEditDate'>

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122374

You're missing some dollar signs - it should be

<xsl:when test='$OrderCreateDate != $OrderEditDate'>

Your current test is trying to find elements called OrderCreateDate and OrderEditDate as children of the current context node, which I presume (given your variable definitions) don't exist. Thus it's checking whether '' != '', and taking the otherwise branch.

Upvotes: 2

Related Questions