[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Date Difference

Subject: Re: Date Difference
From: Chris Burdess <d09@xxxxxxx>
Date: Tue, 20 Dec 2005 09:55:35 +0000
xsl date diff
Haarman, Michael wrote:
If you want your dates to have "dateness", XPath 2.0 is the way to
go, and Mike H has shown the way forward.

As much as to say "over yonder". Wendell reminds me that I failed to account for single digit days, as well. It matters because the cast to xs:date demands a normalized, standardized and reliable lexical form:

YYYY-MM-DD

FWIW, the following stylesheet, much revised, does just what is
required and
nothing more, over the following instance XML:

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

<?xml version="1.0"?>
<data>
<dateRange>
<date>9/1/2004</date>
<date>10/25/2005</date>
</dateRange>
</data>

Here's an example that works with XSLT 1.0:


date-diff.xsl:
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/
Transform'>
  <xsl:template match='dateRange'>
    <diff>
      <xsl:call-template name='diff'>
        <xsl:with-param name='m1' select='substring-before(date[1],
"/")'/>
        <xsl:with-param name='d1' select='substring-before(substring-
after(date[1], "/"), "/")'/>
        <xsl:with-param name='y1' select='substring-after(substring-
after(date[1], "/"), "/")'/>
        <xsl:with-param name='m2' select='substring-before(date[2],
"/")'/>
        <xsl:with-param name='d2' select='substring-before(substring-
after(date[2], "/"), "/")'/>
        <xsl:with-param name='y2' select='substring-after(substring-
after(date[2], "/"), "/")'/>
      </xsl:call-template>
    </diff>
  </xsl:template>
  <xsl:template name='diff'>
    <xsl:param name='m1'/>
    <xsl:param name='d1'/>
    <xsl:param name='y1'/>
    <xsl:param name='m2'/>
    <xsl:param name='d2'/>
    <xsl:param name='y2'/>
    <xsl:value-of select='(number($d1) + number(document("days.xml")/
days/year[@name=$y1]/month[$m1]) + number(document("days.xml")/days/
year[@name=$y1]/@offset)) - (number($d2) + number(document
("days.xml")/days/year[@name=$y2]/month[$m2]) + number(document
("days.xml")/days/year[@name=$y2]/@offset))'/>
  </xsl:template>
</xsl:stylesheet>

days.xml:
<days>
  <year name='2004' offset='12419'>
    <month>31</month>
    <month>29</month>
    <month>31</month>
    <month>30</month>
    <month>31</month>
    <month>30</month>
    <month>31</month>
    <month>31</month>
    <month>30</month>
    <month>31</month>
    <month>30</month>
    <month>31</month>
  </year>
  <year name='2005' offset='12784'>
    <month>31</month>
    <month>28</month>
    <month>31</month>
    <month>30</month>
    <month>31</month>
    <month>30</month>
    <month>31</month>
    <month>31</month>
    <month>30</month>
    <month>31</month>
    <month>30</month>
    <month>31</month>
  </year>
</days>

You can of course generate days.xml dynamically.
--
g
, Chris Burdess
  "They that can give up essential liberty to obtain a little safety
  deserve neither liberty nor safety." - Benjamin Franklin

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.