Name
[2.0]
years-from-duration()
Given an xs:duration value,
returns the number of years in that duration.
Syntax
xs:integer?years-from-duration(xs:duration?)
Inputs
An xs:duration
value.
Output
An xs:integer
representing the years component of the given xs:duration. Be aware that for an
xs:dayTimeDuration, this
function always returns 0
because there is no years component of an xs:dayTimeDuration. Also, if the
argument is the empty sequence, this function returns the empty
sequence.
Defined in
XQuery 1.0 and XPath 2.0 Functions and Operators section 10.5, “Component Extraction Functions on Durations, Dates and Times.”
Example
This stylesheet demonstrates the years-from-duration() function with
all three types of durations:
<?xml version="1.0"?>
<!-- years-from-duration.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>
Extracting the years component from durations:</xsl:text> <xsl:variable name="sampleDuration" as="xs:duration" select="xs:duration('P3Y8M2DT4H23M12.2S')"/> <xsl:variable name="sampleYearMonthDuration" as="xs:yearMonthDuration" select="xs:yearMonthDuration('P3Y8M')"/> <xsl:variable name="sampleDayTimeDuration" as="xs:dayTimeDuration" select="xs:dayTimeDuration('P2DT4H23M12.2S')"/> <xsl:text>

 A sample xs:duration: </xsl:text> <xsl:value-of select="$sampleDuration"/> <xsl:text>
 The years component of this duration is ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access