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 February 26th, 2008, 02:55 PM
Registered User
 
Join Date: Feb 2008
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!

 
Old February 27th, 2008, 05:23 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 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 -/
 
Old February 27th, 2008, 06:08 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing duplicate entries avantikad XSLT 2 January 2nd, 2008 12:29 PM
Removing duplicate Value Row milindsaraswala Excel VBA 0 November 14th, 2007 06: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 07:37 PM





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