Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2009, 07:48 AM
Registered User
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow Unique nodes based on two attributes

Hi All,
I have been trying with this issue for a couple of days now, I will appreciate any help.

Following is the xml and I need unique rows based on two attribute i.e "Organization" and "Event". I have been trying to constract an IF condition which tells me whether the current node is a duplicate based on above mentioned attributes under a loop.

<?xml version="1.0" encoding="UTF-8"?>
<Rows>
<Row Title="Paul Smith" Role="Finance Director" Organization="London Trust" Organization_x0020_Type="Trust" Description=" " Event="Communication Procedures" >
</Row>
<Row Title="John Martin" Role="" Organization="Oxford Deanery" Organization_x0020_Type="Deanery" Description=" " Event="Communication Procedures" >
</Row>
<Row Title="Abdul Waheed" Role="" Organization="Oxford Deanery" Organization_x0020_Type="Deanery" Description=" " Event="Communication Procedures" >
</Row>
<Row Title="Jojo" Role="IT Head" Organization="Oxford Deanery" Organization_x0020_Type="Deanery" Description=" " Event="Joint IT infrastructure" >
</Row>
<Row Title="Joe Black" Role="" Organization="London Trust" Organization_x0020_Type="Trust" Description="" Event="Data sharing standards" >
</Row>
<Row Title="Peter Crouch" Role="" Organization="Oxford Deanery" Organization_x0020_Type="Deanery" Description="" Event="Data sharing standards" >
</Row>
<Row Title="Steven King" Role="" Organization="Northern Deanery" Organization_x0020_Type="Deanery" Description="" Event="Data sharing standards" >
</Row>
<Row Title="David Osborne" Role="" Organization="Oxford Deanery" Organization_x0020_Type="Deanery" Description="" Event="Hardware for data sharing" >
</Row>
<Row Title="David Brooks" Role="" Organization="Northern Deanery" Organization_x0020_Type="Deanery" Description="" Event="Hardware for data sharing" >
</Row>
</Rows>
 
Old August 2nd, 2009, 06:39 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you use XSLT 2.0 then you can simply use xsl:for-each-group to group by those attribute values and then process or output the first item in each group e.g. this stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="Rows">
    <xsl:for-each-group select="Row" group-by="concat(@Organization, '|', @Event)">
      <xsl:copy-of select="."/>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>
when applied to your input yields
Code:
<Row Title="Paul Smith" Role="Finance Director" Organization="London Trust"
     Organization_x0020_Type="Trust"
     Description=" "
     Event="Communication Procedures">
</Row>
<Row Title="John Martin" Role="" Organization="Oxford Deanery"
     Organization_x0020_Type="Deanery"
     Description=" "
     Event="Communication Procedures">
</Row>
<Row Title="Jojo" Role="IT Head" Organization="Oxford Deanery"
     Organization_x0020_Type="Deanery"
     Description=" "
     Event="Joint IT infrastructure">
</Row>
<Row Title="Joe Black" Role="" Organization="London Trust"
     Organization_x0020_Type="Trust"
     Description=""
     Event="Data sharing standards">
</Row>
<Row Title="Peter Crouch" Role="" Organization="Oxford Deanery"
     Organization_x0020_Type="Deanery"
     Description=""
     Event="Data sharing standards">
</Row>
<Row Title="Steven King" Role="" Organization="Northern Deanery"
     Organization_x0020_Type="Deanery"
     Description=""
     Event="Data sharing standards">
</Row>
<Row Title="David Osborne" Role="" Organization="Oxford Deanery"
     Organization_x0020_Type="Deanery"
     Description=""
     Event="Hardware for data sharing">
</Row>
<Row Title="David Brooks" Role="" Organization="Northern Deanery"
     Organization_x0020_Type="Deanery"
     Description=""
     Event="Hardware for data sharing">
</Row>
If you use XSLT 1.0 then you can use Muenchian grouping:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:key name="k1" match="Row" use="concat(@Organization, '|', @Event)"/>
  
  <xsl:template match="Rows">
    <xsl:for-each select="Row[generate-id() = generate-id(key('k1', concat(@Organization, '|', @Event))[1])]">
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 11th, 2009, 07:38 AM
Registered User
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin,
Sorry for the late reply, I went on holiday.
I am using XSLT 1.0, and your solution works great in my test application, but I am not able to make it work in my SharePoint page.

I need to use it as;
<xsl:template name="dvt_1.body">
<xsl:param name="Rows"/>
<xsl:for-each select="Row[generate-id() = generate-id(key('k1', concat(@Organization, '|', @Event))[1])]">
<xsl:call-template name="dvt_1.rowview"/>
</xsl:for-each>
</xsl:template>

I added
<xsl:key name="k1" match="Row" use="concat(@Organization, '|', @Event)"/>

on top in the beginning of stylesheet.
The template inside foreach loop never gets called.
I will appreciate your help.
Regards





Similar Threads
Thread Thread Starter Forum Replies Last Post
Display only unique Nodes kylektran XSLT 1 July 17th, 2009 08:41 AM
XSLT:How to loop through nodes and retrieve attributes values based on some condition smandeh XSLT 2 March 24th, 2009 01:50 PM
How to select attributes in previous nodes JoshC XSLT 2 March 8th, 2009 05:40 PM
selecting nodes that lack certain attributes rmers XSLT 3 October 4th, 2006 03:44 PM
Need to copy all nodes, attributes, and namespaces juaniux XSLT 4 October 22nd, 2004 04:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.