xsl-list
[Top] [All Lists]

Grouping and avoiding duplicates

2003-12-04 04:00:22
Hello all!

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

My input XML file is of the form:

input.xml
---------

<scl1001>
  ...
  <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>
  ...
  <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>
  <reg date="2003-11-10">
     <user dept="2" lastacc="2003-11-10 10:50:40.438368">tom</usw>
  </reg>
  ...
</scl1001>

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
form:

output.xml
----------

<scl1001>
  ...
  <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>
  ...
</scl1001>

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.

Thanks!


Sample output.xml:
------------------

<scl1001>
  ...
  <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>
  ...
</scl1001>

This is the XSL file:

month_activity.xsl:
-------------------

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

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

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



************ 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
information.
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
prohibited.
************************************************************************** 



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



<Prev in Thread] Current Thread [Next in Thread>
  • Grouping and avoiding duplicates, Aitor San Juan <=