|
Summary
How to easily restructure a flat, delimited XML into a structured (parent/child) XML.
Occassionally we have to deal with XML supplied in a format that isn't particularly elegant or well structured. One such example of this is the flat delimited structure, e.g.
<?xml version=1.0?> <content> <a id=1>content</a> <a id=2>content</a> <a id=3>content</a> <b/> <a id=4>content</a> <b/> <a id=5>content</a> <a id=6>content</a> </content>
Notice that all the <a> and <b> elements are at the same level, but the <a> elements are delimited with interspersed <b> elements. So the XML may need to be transformed into something a little more structured, e.g.
<?xml version=1.0?> <content> <b> <a id=1>content</a> <a id=2>content</a> <a id=3>content</a> </b> <b> <a id=4>content</a> </b> <b> <a id=5>content</a> <a id=6>content</a> </b> </content>
This task is more common than it would at first appear - and I've seen it asked several times on various newsgroups and discussion lists (so there are some systems out there that are producing some fairly poorly designed data structures - what's new!).
I've also seen some fairly complex peices of code for dealing with this scenario - but sometimes complex is an overkill. It struck me that possibly the best way to group the items by a delimiter would be to use a variation on the Muenchian Technique. By using an <xsl:key> with a @use value of generate-id() of the following sibling delimiter element it should be possible to use a single for each to group the elements in one pass. The example XML and XSL below demonstrates this.
Cheers - Marrow http://www.MarrowSoft.com - home of Xselerator (XSLT IDE and debugger) http://www.TopXml.com/Xselerator
| Further additional information | |
Updating comments...
Updating comments...
|