Altova Mailing List Archives


RE: [xsl] Date Difference

From: "Haarman, Michael" <mhaarman@--------->
To:
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:

testDate2.xml:
--------------

<?xml version="1.0"?>
<data>
<dateRange>
<date>9/11/2004</date>
<dateStandard>2004-09-11</dateStandard>
<date>10/25/2005</date>
<dateStandard>2005-10-25</dateStandard>
</dateRange>
</data>

testDate2.xsl:
--------------

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

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  version="2.0"
  exclude-result-prefixes="xs fn">

  <xsl:template match="/">
    <output>
      <xsl:apply-templates/>
    </output>
  </xsl:template>

  <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:choose>
        <xsl:when test="fn:string-length(substring-before(date[1], '/')) =
1">
          <xsl:value-of select="concat('0',date[1])"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="date[1]"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="laterPadded">
      <xsl:choose>
        <xsl:when test="fn:string-length(substring-before(date[2], '/')) =
1">
          <xsl:value-of select="concat('0',date[2])"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="date[2]"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

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


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

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

</xsl:stylesheet>



Using Saxon 8, this yields the desired result:

result.xml
--------------

<?xml version="1.0" encoding="UTF-8"?>
<output>
<durationLaterFromEarlier>P409D</durationLaterFromEarlier><durationEarlierFr
omLater>-P409D</durationEarlierFromLater><durationLaterFromEarlier>P409D</du
rationLaterFromEarlier><durationEarlierFromLater>-P409D</durationEarlierFrom
Later>
</output>



Questions:

A)
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:

<quote>
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.
</quote>

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.

B)
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')
	as="xs:date"/>

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, '(.*)/(.*)/(.*)',
'$3-$1-$2'))"/>

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

C)
Any idiomatic corrections to the stylesheet or notational shortcuts I
missed?


TIA,

Mike

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

Disclaimer

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 www.altova.com/list/index.html. 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.