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

[xsl] Getting years from duration


Subject: [xsl] Getting years from duration
From: Martin Holmes <mholmes@xxxxxxx>
Date: Fri, 07 Jun 2013 20:26:47 -0700

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