Reputation: 89
I have an XSL version 2.0 to remove duplicates using <xsl:sequence>
. I'm simply removing duplicate Entry elements from the below xml base on ID. In addition, I need to consider the date field and remove the older one. Alternately, I could remove the one which is not the current date since the new Entry is always stamped as of the current date. In this case, remove the Entry for ID aces dated 2016-09-01 (bolded). Bear in mind, I do not want to remove an Entry that is not a dup, regardless of date.
<?xml version='1.0' encoding='utf-8'?>
<Data>
<Entry>
<ID>aces</ID>
<Info>
<Code2>0006</Code2>
<AMOUNT>7.52<AMOUNT>
<Info>
<Date>2016-12-18-05:00</Date>
</Entry>
<Entry>
<ID>chawk</ID>
<Info>
<Code2>0007</Code2>
<AMOUNT>7.53<AMOUNT>
<Info>
<Date>2016-11-01-05:00</Date>
</Entry>
**<Entry>
<ID>aces</ID>
<Info>
<Code2>0006</Code2>
<AMOUNT>7.53<AMOUNT>
<Info>
<Date>2016-09-01-05:00</Date>
</Entry>**
</Data>
Here is my current XSL:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<Data xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:for-each-group select="Entry" group-by="ID">
<xsl:sequence select="."/>
</xsl:for-each-group>
</Data>
</xsl:template>
</xsl:stylesheet>
Thanks in advance for your help.
Upvotes: 0
Views: 174
Reputation: 167696
Sort the items in a group by date and output only the latest item (last in ascending sort or first in descending sort order):
<xsl:template match="/*">
<Data xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:for-each-group select="Entry" group-by="ID">
<xsl:for-each select="current-group()">
<xsl:sort select="Date" order="descending"/>
<xsl:if test="position() eq 1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each-group>
</Data>
</xsl:template>
Upvotes: 2