I need to keep the most recent entries from an input xml grouping by a key value. Is it possible to do it with xsl 2.0? Input
<root>
<line>
<date>2021-01-01T00:00:00</date>
<field1>AAA</field1>
<field2>1</field2>
</line>
<line>
<date>2021-01-01T23:00:00</date>
<field1>BBB</field1>
<field2>1</field2>
</line>
<line>
<date>2021-01-02T00:00:00</date>
<field1>CCC</field1>
<field2>2</field2>
</line>
Output should be
<root>
<line>
<date>2021-01-01T23:00:00</date>
<field1>BBB</field1>
<field2>1</field2>
</line>
<line>
<date>2021-01-02T00:00:00</date>
<field1>CCC</field1>
<field2>2</field2>
</line>
The xsl keeps the most recent entry grouping by field2, so the result is two entries (field2= 1 and field2=2).
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each-group select="root/line" group-by="field2">
<xsl:sort select="date"/>
<line>
<xsl:copy-of select="node()"/>
</line>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
current-group()
and usingxsl:sort
or you can just select the item in thecurrent-group()
with themax(current-group()/date/xs:dateTime(.)) = xs:dateTime(date)
. – Martin Honnenversion="1.0"
. – michael.hor257k