1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xpath-year-from-date.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

2.7 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
year-from-date() Extracts the year component from an xs:date value as an xs:integer. 2026-04-18T00:00:00Z 2.0 XSLT 2.0 date function year-from-date(date)
xslt
reference
xpath
xslt2

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 with xs: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() and day-from-date().
  • For xs:dateTime values, use year-from-dateTime() instead.

See also