xsl-list
[Top] [All Lists]

HOWTO: convert flat list w/ level information to a hierarchial one?

2003-12-02 03:40:58
Dear XSLT Community!

I couldn't solve a problem of class "flat file transform".

From (@name, just for illustration) flat file with @level information:
<node>
 <node level="0" type="c" name="toplevel"/>
 <node level="1" type="i" name="1. item"/>
 <node level="1" type="c" name="2. container"/>
 <node level="2" type="i" name="2.1 item"/>
 <node level="2" type="i" name="2.2 item"/>
 <node level="1" type="i" name="3. item"/><!-- implicit close of previous 
container -->
 <node level="1" type="c" name="4. container"/>
 <node level="2" type="i" name="4.1 item"/>
 <node level="2" type="c" name="4.2 container"/>
 <node level="3" type="i" name="4.2.1 item"/>
</node>
<!--
@type = 'c' ... container
@type = 'i' ... Item
-->

transform to:
<node>
 <node type="c" name="toplevel">
  <node type="i" name="1. item"/>
  <node type="c" name="2. container"/>
   <node type="i" name="2.1 item"/>
   <node type="i" name="2.2 item"/>
  </node>
  <node type="i" name="3. item"/>
  <node type="c" name="4. container">
   <node type="i" name="4.1 item"/>
   <node type="c" name="4.2 container"/>
    <node type="i" name="4.2.1 item"/>
   </node>
  </node>
 </node>
</node>

What I tried:
Calling recursively a template with passing parameters $node, $last-level, but 
I failed to leave recursion in a proper way;-(

Any pointer|hint|solution for a (ideally) XSLT1.0 approach is highly 
appreciated!

Thanks in advance
             Marcus

P.S. I read http://www.dpawson.co.uk/xsl/sect2/flatfile.html

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list