dedup in xslt

Posted on 2005-04-21
Last Modified: 2013-11-18

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"/>

Question by:ChenChen
    1 Comment
    LVL 5

    Accepted 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="" xmlns: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

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    A short article about a problem I had getting the GPS LocationListener working.
    HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now