XSLT Reference
year-from-date()
Extracts the year component from an xs:date value as an xs:integer.
Syntax
year-from-date(date)Description
year-from-date() returns the year component of an xs:date value as an xs:integer. For proleptic Gregorian calendar dates, years before the common era are represented as non-positive integers (year 1 BCE = 0, year 2 BCE = -1, etc.).
If the argument is the empty sequence, the empty sequence is returned.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
date | xs:date? | Yes | The date value from which to extract the year. |
Return value
xs:integer? — the year component of the date, or the empty sequence if the argument is the empty sequence.
Examples
Extract the year from date attributes
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<publications>
<book date="2020-03-15">Learning XSLT</book>
<book date="2023-11-01">XPath in Practice</book>
</publications>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<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="xml" indent="yes"/>
<xsl:template match="/publications">
<years>
<xsl:for-each select="book">
<year title="{.}"><xsl:value-of select="year-from-date(xs:date(@date))"/></year>
</xsl:for-each>
</years>
</xsl:template>
</xsl:stylesheet>
Output:
<years>
<year title="Learning XSLT">2020</year>
<year title="XPath in Practice">2023</year>
</years>
Filter items from the current year
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<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="xml" indent="yes"/>
<xsl:template match="/publications">
<current-year-books>
<xsl:copy-of select="book[year-from-date(xs:date(@date)) = year-from-date(current-date())]"/>
</current-year-books>
</xsl:template>
</xsl:stylesheet>
Notes
- The argument must be typed as
xs:date, not a plain string. Cast withxs:date(@attr)when reading attribute values. - To get the current year, use
year-from-date(current-date()). - Companion functions for the other date components are
month-from-date()andday-from-date(). - For
xs:dateTimevalues, useyear-from-dateTime()instead.