How to get latest year in xslt

###XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<publications>
<publication>
<author>Fan</author>
<year>1999</year>
<title>test1</title>
</publication>
<publication>
<author>Fan</author>
<year>2012</year>
<title>test2</title>
</publication>
<publication>
<author>Fan</author>
<year>1980</year>
<title>test3</title>
</publication>
<publication>
<author>Fan</author>
<year>2007</year>
<title>test4</title>
</publication>
</publications>

###XSLT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
<!-- get latest year -->
<xsl:for-each select="/publications/publication/year">
<xsl:variable name="currentYear" select="normalize-space(text())"/>
<xsl:if test="self::node()[count(../..//year[. &gt; $currentYear])=0]">
<latestYear>
<xsl:value-of select="$currentYear"/>
</latestYear>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Share