Re: [xsl] Display unique values?

Subject: Re: [xsl] Display unique values?
From: George Cristian Bina <george@xxxxxxxxxxxxx>
Date: Fri, 20 Jan 2006 10:52:43 +0200
Hi Alan,

Your stylesheet looks complicated, how about:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output version="1.0" encoding="UTF-8" indent="no" omit-xml-declaration="no"
media-type="text/html"/>


  <xsl:template match="/">
    <html><table>
      <thead><tr>
        <td>Event</td><td>Device</td>
      </tr></thead>
      <tbody>
        <xsl:apply-templates/>
      </tbody>
    </table></html>
  </xsl:template>

<xsl:template match="Event">
<tr>
<td><xsl:value-of select="ID"/></td>
<td>
<xsl:for-each select="EventDevicesArchive/EventDevice/Device[not(text()=../preceding-sibling::EventDevice/Device/text())]">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>


Hope that helps,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Alan Fenn wrote:
I'm an XSLT newbie, and have been struggling with
trying to extract unique values from an XML doc. I've
read Jeni's pages on the Muenchian technique, plus
some posts on the lists, but I can't find anything
that specifically relates to this.

Here's my XML snippet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="sample.xslt"?>
<NewDataSet>
  <Archive>
    <Event>
      <ID>159</ID>
      <EventDevicesArchive>
        <EventDevice>
          <Device>2786</Device>
        </EventDevice>
        <EventDevice>
          <Device>2786</Device>
        </EventDevice>
      </EventDevicesArchive>
    </Event>
    <Event>
      <ID>160</ID>
      <EventDevicesArchive>
        <EventDevice>
          <Device>903</Device>
        </EventDevice>
        <EventDevice>
          <Device>904</Device>
        </EventDevice>
        <EventDevice>
          <Device>903</Device>
        </EventDevice>
      </EventDevicesArchive>
     </Event>
    <Event>
      <ID>161</ID>
      <EventDevicesArchive>
        <EventDevice>
          <Device>2786</Device>
        </EventDevice>
        <EventDevice>
          <Device>2787</Device>
        </EventDevice>
        <EventDevice>
          <Device>2787</Device>
        </EventDevice>
      </EventDevicesArchive>
    </Event>
    <Event>
      <ID>162</ID>
      <EventDevicesArchive>
        <EventDevice>
          <Device>903</Device>
        </EventDevice>
        <EventDevice>
          <Device>904</Device>
        </EventDevice>
        <EventDevice>
          <Device>903</Device>
        </EventDevice>
      </EventDevicesArchive>
     </Event>
  </Archive>
</NewDataSet>

and here's the XSLT I've tried:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:fn="http://www.w3.org/2005/02/xpath-functions";
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes";>
<xsl:output version="1.0" encoding="UTF-8" indent="no"
omit-xml-declaration="no" media-type="text/html" />
<xsl:key name="IncidentID" match="Event" use="ID" />
<xsl:key name="DeviceID" match="Event" use="concat(ID,
' ', EventDevicesArchive/EventDevice/Device)" />
<xsl:template match="/">
<html>
<table>
<thead>
<tr>
<td>Event</td>
<td>Device</td>
</tr>
</thead>
<tbody>
<xsl:for-each
select="NewDataSet/Archive/Event">
<tr>
<td>
<xsl:value-of select="ID"/>
</td>
<td>
<xsl:variable name="DeviceItems"
select="key('IncidentID', ID)" />
<xsl:for-each
select="EventDevicesArchive/EventDevice">
<xsl:for-each
select="$DeviceItems[generate-id() =
generate-id(key('DeviceID', concat(ID, ' ',
EventDevicesArchive/EventDevice/Device))[1])]">
<xsl:if test="position()!=1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="Device" />
</xsl:for-each>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</tbody>
</table> </html>
</xsl:template>
</xsl:stylesheet>


The desired output is:

ID Device
159 2786
160 903, 904
161 2786, 2787
162 903, 904
With the above, all I get is:


ID Device
159 160 161 162


I've also tried variations that return all the
devices, and a list of devices that are unique to the
entire document, not each event. Any ideas?


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

Current Thread