[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Getting years from duration


Subject: RE: [xsl] Getting years from duration
From: Michele R Combs <mrrothen@xxxxxxx>
Date: Sat, 8 Jun 2013 20:42:04 +0000

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@xxxxxxx]
Sent: Friday, June 07, 2013 11:26 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
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


Current Thread
Keywords