Subject: Re: [xsl] Date Difference From: Chris Burdess <d09@xxxxxxx> Date: Tue, 20 Dec 2005 09:55:35 +0000 |
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>
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Date Difference, Haarman, Michael | Thread | RE: [xsl] Date Difference, Haarman, Michael |
Re: [xsl] JDK 5 XSLTC handling of n, drkm | Date | RE: [xsl] Date Difference, drkm |
Month |