Altova Mailing List Archives

RE: [xsl] Date Difference

From: "Haarman, Michael" <mhaarman@--------->
Date: 12/19/2005 9:35:00 PM
Three questions below.

> From: David Carlisle 
> > I basically have two strings :   "9/11/2004"  and "10/25/2005"
> > and would like to produce the number of days between the two.
> I didn't know there was a month 25.

(M)M/DD/YYYY is a standard lexical representation in America.

> XSLT2 has date arithmetic built in but only if you have your 
> dates in a
> standard format

I had a moment this morning and have been meaning to *get a round tuit* and
begin with XSLT2.  Reading the OP I said to myself: here is a simple and
illustrative chore to undertake.  I have raised a few questions.

I built an XML instance using the OP's datapoints:


<?xml version="1.0"?>


<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl=""
  exclude-result-prefixes="xs fn">

  <xsl:template match="/">

  <xsl:template match="*/dateRange">
    <!-- normalized data points -->
    <xsl:variable name="earlier" select="dateStandard[1]" as="xs:date"/>
    <xsl:variable name="later" select="dateStandard[2]" as="xs:date"/>

    <!-- padding month to two digits -->
    <xsl:variable name="earlierPadded">
        <xsl:when test="fn:string-length(substring-before(date[1], '/')) =
          <xsl:value-of select="concat('0',date[1])"/>
          <xsl:value-of select="date[1]"/>
    <xsl:variable name="laterPadded">
        <xsl:when test="fn:string-length(substring-before(date[2], '/')) =
          <xsl:value-of select="concat('0',date[2])"/>
          <xsl:value-of select="date[2]"/>

    <!-- get standardized date strings -->
    <xsl:variable name="regexE" 
      select="xs:date(fn:replace($earlierPadded, '(.*)/(.*)/(.*)',
    <xsl:variable name="regexL"
      select="xs:date(fn:replace($laterPadded, '(.*)/(.*)/(.*)',

      <xsl:value-of select="$later - $earlier"/>
      <xsl:value-of select="$earlier - $later"/>

      <xsl:value-of select="$regexL - $regexE"/>
      <xsl:value-of select="$regexE - $regexL"/>


Using Saxon 8, this yields the desired result:


<?xml version="1.0" encoding="UTF-8"?>


Is there a reason the operators described within the op: namespace in the CR
are not available?  I began with statements like:

op:subtract-dates($earlier, $later)

and only much later found the sentences clarifying these operators:

Functions defined with the op prefix are described here to underpin the
definitions of the operators in [XML Path Language (XPath) 2.0], [XQuery
1.0: An XML Query Language] and [XSL Transformations (XSLT) Version 2.0].
These functions are not available directly to users, and there is no
requirement that implementations should actually provide these functions.

The functional notation seems much more intuitive to me than using
arithmetic operations on dates.  Which is the greater value?  Simple math on
date values was drummed out of me very early in my grade-school career.

This statement:

    <xsl:variable name="later" select="dateStandard[2]" as="xs:date"/>

seemed to work fine, but this one later:

    <xsl:variable name="regexE" 
      select="fn:replace($earlierPadded, '(.*)/(.*)/(.*)', '$3-$1-$2')

threw an error when the value of $regexE was used in the later arithmetic,
complaining that it was still a string.  Stating it in the following manner
corrected the error:

    <xsl:variable name="regexE" 
      select="xs:date(fn:replace($earlierPadded, '(.*)/(.*)/(.*)',

I suspect I am missing an implicit cast somewhere that makes the first
statement work and renders the as= attribute moot.

Any idiomatic corrections to the stylesheet or notational shortcuts I



Mike Haarman,
XSL Developer,
Internet Broadcasting Systems, Inc.


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.