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 March 9th, 2012, 03:53 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old March 9th, 2012, 04:09 PM
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: 42%
Activity: 42% Activity: 42% Activity: 42%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,826
Thanks: 0
Thanked 263 Times in 258 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
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 nodes nguna XSLT 6 February 16th, 2009 09:40 AM
Removing duplicate edge elements geoGregory3 XSLT 2 February 27th, 2008 05:08 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



All times are GMT -4. The time now is 04:37 AM.


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