BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


XSLT
Hex and Binary Conversions in XSLT - with Boolean math functions
Using XSLT to produce SVG (1)
Using XSLT to produce SVG (2)
Performance Tuning XSLT - Part 1
Performance Tuning XSLT - Part 2
Performance Tuning XSLT - Part 3
Performance Tuning XSLT - Part 4
Using XSLT to produce SVG (3)
XPath as parameters
Table with more than one column
Create an N-Column Table from XML node-set
Finding ""the corresponding node"" in a parallel subtree
Get the last item from a delimited list
Limitation to Muenchian method.
Find the number of tokens or words in a string
Randomization of node-sets or node-lists
XPath Analyzer
Finding the Maximum Date
World Cup 2002 fever hits XSLT!!
Using XSLT for DHTML Menus


 
 

<< XQuery.NET and XML >>


By Martin Rowlinson (aka Marrow)
First Posted 03/07/2002
Times viewed 235

Restructuring flat delimited XML


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

 

 

Additional information

Further additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< XQuery.NET and XML >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML