[xsl] Removing duplicates after list is sorted

Subject: [xsl] Removing duplicates after list is sorted
From: Nicole Flaten <nlflaten@xxxxxxxxxxxx>
Date: Wed, 10 May 2006 10:39:01 -0700
Hi,

I am learning XSL and have been struggling with the same question for a 
couple of days.  It is probably a simple question to someone familiar with 
XSL so I was hoping to get some help.  Below are portions of my XSL and 
XML files.  Basically, for each different base (ELS, LAN, KAD) I want to 
display only one record in a html page, the one record is the one with the 
highest CREATIONTIME value.  I have tried many methods and none of them 
has worked.  Below is the latest and almost successful method.  It sorts 
the list in the way I want.  and then in <template match="Report"> I try 
to remove the duplicates so only the first base from the sorted list is 
displayed.  The problem is that I am not able to apply the preceding node 
to the sorted list created in the XSL file, the XSL just applies the 
preceding node test to the XML file.  How can I sort the XML file and the 
apply the preceding node test to the sorted list?  Or is there a better 
method altogether?  Any help would be greatly appreciated.

Thanks, Nicole

<xsl:template match="/">

<html>
        <body>
 
                <table border="1" cellspacing="1" cellpadding="1" 
align="center">
                        <tr class="formtitle" align="center">
                                <td>Base Status</td>
                                <td>Creation Time</td>
                                <td>ASC</td>
                        </tr>
 
 
                        <xsl:apply-templates select="//Document/Report">
                                <xsl:sort order='ascending' 
select='@base'/>
                                <xsl:sort order='descending' 
select='../@CREATIONTIME'/>
                        </xsl:apply-templates> 

                </table>
        </body>
</html>


</xsl:template>

<xsl:template match="Report">
        <xsl:if test="not ((preceding::*[1]/@base)=@base) ">
                <xsl:call-template name="print"/>
        </xsl:if>
</xsl:template>

<xsl:template name="print">
        <tr class="formtext">
                <td class="formtitle">print <xsl:value-of 
select='@base'/></td>
                <td><xsl:value-of select='../@CREATIONTIME'/></td>
        </tr>
        <tr class="formtext">
                <td class="formtitle" align="right">Authorized</td>
                <td align="center"><xsl:value-of 
select="Authorized/@ASC"/></td>
        </tr>
</xsl:template> 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Sample XML data

<?xml-stylesheet href="SparesCalc.xsl" type="text/xsl" ?>
<?xml version="1.0" encoding="UTF-8" ?> 
<docs>
<Document CREATIONTIME="200605040811">
<Report ID="759b" base="ELS">
        <Authorized ASC="2"/> 
</Report>
</Document>
<Document CREATIONTIME="20060404115819">
<Report ID="759a" base="LAN">
        <Authorized ASC="0"/> 
</Report>
</Document>
<Document  CREATIONTIME="200605041506">
<Report ID="759c" base="LAN">
        <Authorized ASC="0"/> 
</Report>
</Document>
<Document CREATIONTIME="200605041507">
<Report ID="759d" base="ELS">
        <Authorized ASC="2"/> 
</Report>
</Document>
<Document CREATIONTIME="20060503111352">
<Report ID="759e" base="KAD">
        <Authorized ASC="4"/> 
</Report>
</Document>
</docs>

Current Thread