Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 26th, 2008, 01:55 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Removing duplicate edge elements

Hi,
This is my first post here. I have the following:
<graph>
    <nodes>
    <node>A</node>
    <node>B</node>
    <node>C</node>
    <node>D</node>
    <node>E</node>
    </nodes>
    <edges>
    <edge source="A" dest="B"/>
    <edge source="A" dest="C"/>
    <edge source="A" dest="D"/>
    <edge source="D" dest="E"/>
    <edge source="A" dest="C"/>
    <edge source="C" dest="D"/>
    <edge source="A" dest="B"/>
    </edges>
</graph>

As you can see, there are duplicate edges (e.g. edges A --> B and A --> C occur twice). I'd like to know how I go about removing these duplicate edges using xslt 1.0 (or 2.0 if not possible in 1.0) such that I have the following:

<graph>
    <nodes>
    <node>A</node>
    <node>B</node>
    <node>C</node>
    <node>D</node>
    <node>E</node>
    </nodes>
    <edges>
    <edge source="A" dest="B"/>
    <edge source="A" dest="C"/>
    <edge source="A" dest="D"/>
    <edge source="D" dest="E"/>
    <edge source="C" dest="D"/>
    </edges>
</graph>

Thanks!

Reply With Quote
  #2 (permalink)  
Old February 27th, 2008, 04:23 AM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 25%
Activity: 25% Activity: 25% Activity: 25%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 Posts
Default

A simple (but depending on the number of items perhaps not the fastest) might be to do something like:

Code:
<xsl:for-each select="edge">
  <xsl:if test="empty(preceding-sibling::edge[@source=current()/@current and @dest=current()/@dest])">
    <xsl:copy-of select="."/>
  </xsl:if>
</xsl:for-each>
/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old February 27th, 2008, 05:08 AM
mhkay's Avatar
Wrox Author
Points: 17,685, Level: 57
Points: 17,685, Level: 57 Points: 17,685, Level: 57 Points: 17,685, Level: 57
Activity: 37%
Activity: 37% Activity: 37% Activity: 37%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,826
Thanks: 0
Thanked 263 Times in 258 Posts
Default

In XSLT 1.0, elimination of duplicates is best done using Muenchian grouping, see for example http://www.jenitennison.com/xslt/grouping. In XSLT 2.0 it's best done using the new xsl:for-each-group instruction.

Neither handles a composite grouping key directly; you will have to form this using string concatenation. For example in 2.0

<xsl:for-each-group select="edge" group-by="concat(@source, '~', @dest)">
  <xsl:copy-of select="."/>
</xsl:for-each-group>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing duplicate entries avantikad XSLT 2 January 2nd, 2008 11:29 AM
Removing duplicate Value Row milindsaraswala Excel VBA 0 November 14th, 2007 05:11 PM
Removing duplicate nodes post-process QuickSilver002 XSLT 3 April 4th, 2007 03:47 PM
Removing the Duplicate anukagni Access 3 August 31st, 2006 12:57 AM
Removing Duplicate Fields antonides Access 2 December 1st, 2003 06:37 PM



All times are GMT -4. The time now is 07:51 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.