xsl-list
[Top] [All Lists]

RE: [xsl] Getting years from duration

2013-06-08 15:42:08
Do you need the age down to month and day?  If not, rather than manipulating 
the attribute value as a date per se, can you just take the first four 
characters (the year) of each and do a simple subtraction?

Michele
________________________________________
From: Martin Holmes [mholmes(_at_)uvic(_dot_)ca]
Sent: Friday, June 07, 2013 11:26 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Getting years from duration

We came up against what looked like a very simple XPath issue today, and
hit a brick wall with it. Given data that looks like this:

         <person role="author">
           <persName>Milton, John</persName>
           <birth when="1600-12-09"/>
           <death when="1674-11-08"/>
         </person>

we want to calculate the age of the person at death. So we thought:
subtract the death date from the birth date to get a duration, then
extract the years from the duration:

   <xsl:template match="person">

     <xsl:variable name="life" select="xs:date(death/@when) -
xs:date(birth/@when)"/>

     <xsl:variable name="age" select="years-from-duration($life)"/>

     <xsl:text>Age at death: </xsl:text>
     <xsl:value-of select="$age"/>

   </xsl:template>


However, the only value we were able to get back, after trying all
manner of permutations and casts, was zero. It appears that what comes
back from the date subtraction (which I think uses the
op:subtract-dates() operator) is always an xs:dayTimeDuration, and that
cannot AFAIKS be manipulated into anything from which a year can be
extracted.

It's always possible to get the number of days and divide by 365.25, but
it seems strange to have to do that. Given the range of date- and
duration-related functions, I'm sure there must be some better way of
getting the result. Does anyone know?

This is using Saxon 9.4.0.6.

Cheers,
Martin


--~------------------------------------------------------------------
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>
--~--


--~------------------------------------------------------------------
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>