vishal kulkarni
vishal kulkarni

Reputation: 11

How to Count as a 1 day if the dates are same in XSLT

I have scenario which I would like to count as one day if the dates are same.

<wd:Date>2021-08-18</wd:Date>
<wd:Date>2021-08-18</wd:Date>

How can I achieve this using XSLT. So, I want count this a one day if the Date is same.

Upvotes: 1

Views: 79

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66781

You could:

  • use xs:date() to evaluate those values as xs:date
  • then subtract one date from the other to get the difference as xs:dateTimeDuration
  • then use days-from-duration() to return that number of days (which would be zero)
  • apply abs(), such that we don't have to worry about which date was on the left or right hand side and positive or negative values
  • and then add 1 to that value

This stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:template match="/">
    <xsl:text>Days: </xsl:text>
    <xsl:value-of select="abs(days-from-duration(xs:date(/*/*:Date[1]) - xs:date(/*/*:Date[2]))) + 1"/>
  </xsl:template>
</xsl:stylesheet>

Applied to this XML file:

<wd:example xmlns:wd="wd">
    <wd:Date>2021-08-18</wd:Date> 
    <wd:Date>2021-08-18</wd:Date>    
</wd:example>

Produces this output:

Days: 1

Upvotes: 0

Related Questions