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 March 9th, 2012, 04:53 PM
Registered User
 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default removing duplicate elements and collecting all sub nodes together

Hello,

I am a newbee to the xslt and have an awkward problem which I spent many hours to solve but could not reach to a conclusion. Thanks in advance for any help.

I have an xml document like this:

<root>

<ELEMENT id="1" >

<CHILD name="aaa">
<EMPLOYEE>Mark</EMPLOYEE>
<EMPLOYEE>John</EMPLOYEE>
</CHILD>

<CHILD name="bbb">
<EMPLOYEE>Tom</EMPLOYEE>
</CHILD>

</ELEMENT>


<ELEMENT id="2" >

<CHILD name="aaa">
<EMPLOYEE>leo</EMPLOYEE>
<EMPLOYEE>Jason</EMPLOYEE>
</CHILD>


</ELEMENT>

<ELEMENT id="1" >

<CHILD name="aaa">
<EMPLOYEE>Tim</EMPLOYEE>
</CHILD>


</ELEMENT>

</root>

What I try to do is collecting "EMPLOYEE"s having same "ELEMENT" id and "CHILD" name together and removing duplicate "ELEMENT"s and "CHILD"s.

I mean I need only one <ELEMENT id="1" > having only one <CHILD name="aaa"> but with all employees belonging to id=1 name=aaa. Resulting file should look like:


<ELEMENT id="1" >

<CHILD name="aaa">
<EMPLOYEE>Mark</EMPLOYEE>
<EMPLOYEE>John</EMPLOYEE>
<EMPLOYEE>Tim</EMPLOYEE>
</CHILD>

<CHILD name="bbb">
<EMPLOYEE>Tom</EMPLOYEE>
</CHILD>

</ELEMENT>


<ELEMENT id="2" >

<CHILD name="aaa">
<EMPLOYEE>leo</EMPLOYEE>
<EMPLOYEE>Jason</EMPLOYEE>
</CHILD>


</ELEMENT>



How should my xslt code be? Do I need to iterate with a for each loop, or should I apply recursive templates?

Many tnx
 
Old March 9th, 2012, 05:09 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The answer to grouping problems depends strongly on whether you are using XSLT 1.0 or 2.0.

For XSLT 1.0, use Muenchian grouping. Any good XSLT book will cover it. It's conceptually tricky when you first encounter it, but once you've learnt the coding pattern, it's fairly straightforward.

For XSLT 2.0, use the xsl:for-each-group instruction.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
removing duplicate nodes nguna XSLT 6 February 16th, 2009 10:40 AM
Removing duplicate edge elements geoGregory3 XSLT 2 February 27th, 2008 06:08 AM
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





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