xsl-list
[Top] [All Lists]

Re: Date Difference

2005-12-20 02:55:35
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[(_at_)name=$y1]/month[$m1]) + number(document("days.xml")/days/ year[(_at_)name=$y1]/@offset)) - (number($d2) + number(document ("days.xml")/days/year[(_at_)name=$y2]/month[$m2]) + number(document ("days.xml")/days/year[(_at_)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.
--
犬 Chris Burdess
  "They that can give up essential liberty to obtain a little safety
  deserve neither liberty nor safety." - Benjamin Franklin





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>