-
Notifications
You must be signed in to change notification settings - Fork 4
/
dates.years.xsl
81 lines (66 loc) · 2.48 KB
/
dates.years.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:kit="https://hananils.de/xslt/kit"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<!--
* hana+nils · Büro für Gestaltung
* https://hananils.de · buero@hananils.de
-->
<xsl:include href="languages.xsl" />
<!--
* Kit: Year Ranges
*
* This template creates date ranges from a node set of years. If the years are
* a continuous range until today, the template will return "since {$year}".
*
* # Example usage
*
* <xsl:apply-templates select="dates" mode="kit:dates-years" />
-->
<xsl:template match="*" mode="kit:dates-years">
<!-- Create a custom node set with all years sorted ascending -->
<xsl:variable name="sorted">
<xsl:for-each select="item">
<xsl:sort select="." order="ascending" />
<xsl:copy-of select="." />
</xsl:for-each>
</xsl:variable>
<xsl:variable name="years" select="exsl:node-set($sorted)" />
<xsl:choose>
<!-- Consecutive years until current year -->
<xsl:when test="$years/item[last()] - $years/item[1] + 1 = count($years/item) and $years/item[last()] = /data/datetime/today/@year">
<xsl:value-of select="$kit:translate/since" />
<xsl:text> </xsl:text>
<xsl:value-of select="$years/item[1]" />
</xsl:when>
<!-- Other years and year ranges -->
<xsl:otherwise>
<xsl:apply-templates select="$years/item" mode="kit:dates-years-item">
<xsl:sort select="." order="ascending" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="item" mode="kit:dates-years-item">
<xsl:choose>
<!-- Years within a range -->
<xsl:when test="following-sibling::item[1] = text() + 1 and preceding-sibling::item[1] = text() - 1"></xsl:when>
<!-- First year in range -->
<xsl:when test="following-sibling::item[1] = text() + 1">
<xsl:value-of select="." />
<xsl:text>–</xsl:text>
</xsl:when>
<!-- Last year in range or single years -->
<xsl:when test="position() != last()">
<xsl:value-of select="." />
<xsl:text>, </xsl:text>
</xsl:when>
<!-- Very last year -->
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>