[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

dedup in xslt

I was wondering if we can do this in xslt.

for example:

<item id="4" name="abc"/>
<item id="1" name="abc"/>
<item id="2" name="bbc"/>
<item id="3" name="bbc"/>
<item id="0" name="x"/>

how can I sort it by "id"(I know how to do this)  and dedup it on "name" (please help here),
and display the first occurace if there is a dup, like this:

<item id="0" name="x"/>
<item id="1" name="abc"/>
<item id="2' name="bbc"/>

1 Solution
Yes you can do this !

I used a xalan extension, but this is a common one, and you will only have to replace the parts needed with you parser's extension.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="xalan">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:key name="names" match="item" use="@name"/>
      <xsl:template match="x">
            <xsl:element name="x">
                  <xsl:variable name="items">
                        <xsl:for-each select="item">
                              <xsl:sort data-type="number" select="@id" order="ascending"/>
                                    <xsl:copy-of select="." />
                  <xsl:apply-templates select="xalan:nodeset($items)/item[generate-id(.) = generate-id(key('names', @name)[1])]"/>
      <xsl:template match="item">
            <xsl:element name="item">
                  <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
                  <xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now