XML technologies / XSLT / XSLT Cookbook / Selecting and traversing / Ignore duplicate elements
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to select nodes unique within a context according to chosen uniqueness criteria.
Solution
XSLT 1.0
Selecting unique nodes commonly uses the preceding and preceding-sibling (axes). When not all elements are siblings, use preceding. This code produces a unique product list from SalesBySalesPerson.xml:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<products>
<xsl:for-each select="//product[not(@sku=preceding::product/@sku)]">
<xsl:copy-of select="."/>
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>For sibling elements, use preceding-sibling:
<products>
<product sku="10000" totalSales="10000.00"/>
<product sku="10000" totalSales="990000.00"/>
<product sku="10000" totalSales="1110000.00"/>
<product sku="20000" totalSales="50000.00"/>
<product sku="20000" totalSales="150000.00"/>
<product sku="20000" totalSales="150000.00"/>
<product sku="25000" totalSales="920000.00"/>
<product sku="25000" totalSales="2920000.00"/>
<product sku="30000" totalSales="5500.00"/>
<product sku="30000" totalSales="115500.00"/>
<product sku="70000" totalSales="10000.00"/>
</products>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/products ">
<products>
<xsl:for-each select="product[not(@sku=preceding-sibling::product/@sku)]">
<xsl:copy-of select="."/>
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>To avoid preceding, which can be inefficient, move to ancestors that are siblings. Use preceding-sibling there, then return to the nodes being tested:
<xsl:for-each select="//product[not(@sku=../preceding-sibling::*/product/@sku)]">
<xsl:copy-of select="."/>
</xsl:for-each>If elements are sorted so duplicates are adjacent, as with these products, inspect only the immediately preceding sibling:
<xsl:for-each select="/salesperson/product[not(@name=preceding-sibling::product[1]/@name]">
<!-- irgendetwas mit jedem eindeutig genannten Produkt tun -->
</xsl:for-each>XSLT 2.0
In XSLT 2.0, solve this by grouping. Use for-each-group with the uniqueness criterion as the group-by value, and use the current group’s first node as the unique node:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<products>
<xsl:for-each-group select="//product" group-by="@sku">
<xsl:copy-of select="current-group( )[1]"/>
</xsl:for-each-group>
</products>
</xsl:template>
</xsl:stylesheet>Discussion
XSLT 1.0
Extension function node-set( ) also permits:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt=" http://exslt.org">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="products">
<xsl:for-each select="//product">
<xsl:sort select="@sku"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<products>
<xsl:for-each select="exslt:node-set($products)/product">
<xsl:variable name="pos" select="position( )"/>
<xsl:if test="$pos = 1 or not(@sku = $products/preceding-sibling::product[1]/@sku">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>I have never found this faster than using axis preceding. It helps when duplicate tests are nontrivial, for example concatenating two attributes:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt=" http://exslt.org">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="people">
<xsl:for-each select="//person">
<xsl:sort select="concat(@lastname,@firstname)"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<products>
<xsl:for-each select="exslt:node-set($people/)person">
<xsl:variable name="pos" select="position( )"/>
<xsl:if test="$pos = 1 or concat(@lastname,@firstname) != concat(people/person[$pos - 1]/@lastname,people/person[$pos - 1]/@firstname)">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>These examples do not work for duplicate removal:
<xsl:template match="/">
<products>
<xsl:for-each select="//product[not(@sku=preceding::product[1]/@sku)]">
<xsl:sort select="@sku"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</products>
</xsl:template>Do not sort to avoid considering more than the previous element. Axes refer to original document order, even with preceding-sibling. This also fails:
<xsl:template match="/">
<xsl:variable name="products">
<xsl:for-each select="//product">
<!-- hier wurde sort entfernt -->
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<products>
<xsl:for-each select="exsl:node-set($products)/product">
<xsl:sort select="@sku"/>
<xsl:variable name="pos" select="position( )"/>
<xsl:if test="$pos = 1 or @sku != $products/product[$pos - 1]/@sku">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</products>
</xsl:template>It fails because position( ) returns the position after sorting, while $products itself was not sorted, only an inaccessible copy.
XSLT 2.0
Sometimes only adjacent duplicates should be removed. Regularly timed measurements of a stable system may contain many consecutive equal readings. Remove those repetitions while retaining identical readings later in the sequence.
Use xsl:for-each-group again, but with group-adjacent instead of group-by:
<measurements>
<data time="12:00:00" value="1.0"/>
<data time="12:00:01" value="1.0"/>
<data time="12:00:02" value="1.1"/>
<data time="12:00:03" value="1.1"/>
<data time="12:00:04" value="1.0"/>
<data time="12:00:05" value="1.1"/>
<data time="12:00:06" value="1.2"/>
<data time="12:00:07" value="1.3"/>
<data time="12:00:08" value="1.4"/>
<data time="12:00:09" value="1.6"/>
<data time="12:00:10" value="1.9"/>
<data time="12:00:11" value="2.1"/>
<data time="12:00:12" value="1.7"/>
<data time="12:00:13" value="1.5"/>
</measurements>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/measurements">
<xsl:copy>
<xsl:for-each-group select="data" group-adjacent="@value">
<xsl:copy-of select="current-group( )[1]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<!--Ausgabe -->
<measurements>
<data time="12:00:00" value="1.0"/>
<data time="12:00:02" value="1.1"/>
<data time="12:00:04" value="1.0"/>
<data time="12:00:05" value="1.1"/>
<data time="12:00:06" value="1.2"/>
<data time="12:00:07" value="1.3"/>
<data time="12:00:08" value="1.4"/>
<data time="12:00:09" value="1.6"/>
<data time="12:00:10" value="1.9"/>
<data time="12:00:11" value="2.1"/>
<data time="12:00:12" value="1.7"/>
<data time="12:00:13" value="1.5"/>
</measurements>See also
Dave Parsons’s XSLT FAQ describes a key-based solution and related problems.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de