[xsl] Grouping and avoiding duplicates

Subject: [xsl] Grouping and avoiding duplicates
From: Aitor San Juan <asanjuan@xxxxxxxxxxxxxx>
Date: Thu, 4 Dec 2003 12:00:22 +0100
Hello all!

Let's see if anybody can help me with this.

My input XML file is of the form:


  <reg date="2003-11-04">
     <user dept="2" lastacc="2003-11-04 09:13:10.003276">john</user>
     <user dept="2" lastacc="2003-11-04 09:09:23.043209">tom</user>
     <user dept="1" lastacc="2003-11-04 21:31:25.748652">JAMES</user>
  <reg date="2003-11-07">
     <user dept="1" lastacc="2003-11-07 14:33:33.735231">rob</user>
     <user dept="2" lastacc="2003-11-07 18:58:50.221562">julia</user>
     <user dept="1" lastacc="2003-11-07 22:23:57.772418">JAMES</user>
  <reg date="2003-11-10">
     <user dept="2" lastacc="2003-11-10 10:50:40.438368">tom</usw>

The files contains the timestamp of the last login of users on each
day. Each user may log in several times during a day, and may log
in several days. The different <user>'s within each <reg> may not be
ordered. The file contains information of the current year.

I'd like to transform that XML file into another of the following


  <user dept="1" lastacc="2003-11-07 22:23:57.772418">JAMES</user>
  <user dept="2" lastacc="2003-11-10 10:50:40.438368">tom</usw>

Given a year and month (say Nov 2003), output.xml contains the
list of <user> tags, with the most recent login timestamp, thus
each <user> appears just once.

The XSL sheet I'm working on does not exactly generate the
required output, it repeats <user>'s. The problem is in the
first <for-each>.

I don't know how to select distinct <user> tags, that is,
distinct user names (avoid duplicates).

Any hint would be highly appreciated.


Sample output.xml:

  <user dept="2" lastacc="2003-11-10 10:50:40.438368">tom</user>
  <user dept="2" lastacc="2003-11-10 10:50:40.438368">tom</user>

This is the XSL file:


(Note: "theDate" is a parameter of the form yyyy-mm)

<xsl:key name="group-by-name" match="user" use="."/>

<xsl:template match="scl1001">
	<xsl:for-each select=".//user[substring(@lastacc,1,7)=$theDate]">
		<xsl:sort select="." data-type="text" order="ascending"/>
			<xsl:sort select="@lastacc" data-type="text"
			<xsl:if test="position() = 1"><xsl:copy-of

************ LEGEZKO OHARRA / AVISO LEGAL / LEGAL ADVICE *************
Mezu honek isilpeko informazioa gorde dezake, edo jabea duena, edota legez
babestuta dagoena.
Zuri zuzendua ez bada, bidali duenari esan eta ezabatu, inori berbidali
edo gorde gabe,legeak debekatzen duelako mezuak erabiltzea baimenik gabe.
Este mensaje puede contener información confidencial, en propiedad o
legalmente protegida.
Si usted no es el destinatario, le rogamos lo comunique al remitente
y proceda a borrarlo, sin reenviarlo ni conservarlo, ya que su uso no 
autorizado está prohibido legalmente.
This message may contain confidential, proprietary or legally privileged
If you are not the intended recipient of this message, please notify it to
the sender and delete without resending or backing it, as it is legally

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread