I used muenchian grouping,
which is very well explained here
http://www.jenitennison.co
Main Topics
Browse All TopicsDear Experts!
I have an XML file with content which might be described as an repeating sequence with elements all optional.
I want to transform this xml into a nice grouped xml structure.
How do I do?
Regards,
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I used muenchian grouping,
which is very well explained here
http://www.jenitennison.co
Dear Gertone!
Thanks for reply.
Anyway, your solution does work on the sample data I gave, but it does not work in general if you change the values of the elements.
Your solution focus on grouping the values rather than taking the structure into account.
What I am looking for is a solution that will work with the XML Schema structure despite of the actual data content.
I hope you can come up with the brilliant solution on this one too...?
Thanks in advance,
in anticipation,
best regards
Now that I understand what you want,
I would solve this
- in XSLT2
- or in a two pass XSLT1
Since it is only three elements that need grouping, you could test all conditions,
so an approach could be to iterate over all the elements that can start a new group
That is: one, two that doesn't have a direct preceding sibling one and three that doesn't have a direct preceding sibling one or two
If you need an implementation in XSLT1 having more grouping elements, eg. <four> and <five> I recommend you two make this a two step... I can imagine some more elegant approaches
Dear Gertone!
Thanks for reply.
Yes, I should have made it more clear, that my focus was on the Schema construct rather than the data...
Yes, I need a solution for XSLT 1
And yes, I am looking for a generic solution.
How would you do this in two step in XSLT 1?
Actually constructs like this is quite common in XHTML, but a mess when you want to (re)structure it for database tables or the like...
Thanks a lot.
Regards,
In a more general way
I would walk through the child elements recursively,
passing a parameter containing the "history" and a parameter for current group
based on the current element name and the history you could either keep the group or make a new group
and you copy the element but add an attribute with the group
This way you have selected for each node in which group it belongs
The second pass would be a simple muenchian based on the group attribute
You have now seperated the actual grouping (which changes the structure of the document and could give you some issues with nesting)
from the indicator step that decides which group a node belongs too
In XSLT2, you could easily do that in one step because you can list the new-group-triggers in a for-each-group statement
If you are dealing with nesting XHTML, you need to take into account that a h2 nests deeper than a h1... which is yet another issue.
Doing that in XSLT1 would give you a pipeline of processes to be comfortable. It is a piece of cake in XSLT2
If you are realy nesting HTML, I recommend that you evaluate using XSLT2
Well, actually I am happy with the elegance and the robustness of the approach,
I hope you are too
It is not too obvious a task though
Note that grouping is a very tricky business in XSLT1
I have done my bit of XHTML processing and Word-XML processing
and I must say that that is even tricky in XSLT2 :-(
Business Accounts
Answer for Membership
by: GertonePosted on 2009-11-01 at 02:20:28ID: 25713192
Here is all the XSLT you need
Select allOpen in new window