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

Re: [xsl] Getting years from duration

 Subject: Re: [xsl] Getting years from duration From: Martin Holmes Date: Sat, 8 Jun 2013 16:55:52 -0700

We only wanted years, but a plain subtraction won't do, as others have noted; you need to check whether to subtract a year from the result or not, depending on the times of year of the births and deaths. So:

```Born 1950 December 31
Died 2000 January 1```

The plain subtraction will give you 50, but the age is actually 49 (plus a day). [If my math is correct at the end of a LONG day.]

```Cheers,
Martin```

On 13-06-08 01:42 PM, Michele R Combs wrote:
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```

.

```--
Martin Holmes
University of Victoria Humanities Computing and Media Centre
(mholmes@xxxxxxx)```