XSLT- Create a tree from XML



I want to create an XML with a hierarchic structure from a flat XML file. The XML file contains the parent-child relations and if a node is a leaf or not.


There is no explicit information about the level of nesting or the root nodes in the structure. The XML-file can contain more than one root element and it is not ordered.



<list>
<stru><parent>A01</parent><child>P04</child></stru>
<stru><parent>B11</parent><child>P01</child></stru>
<stru><parent>B12</parent><child>P01</child></stru>
<stru><parent>B12</parent><child>P03</child></stru>
<stru><parent>B21</parent><child>P02</child></stru>
<stru><parent>B21</parent><child>P03</child></stru>
<item><cod>B01</cod><isparent>Y</isparent></item>
<item><cod>B11</cod><isparent>Y</isparent></item>
<item><cod>B12</cod><isparent>Y</isparent></item>
<item><cod>B21</cod><isparent>Y</isparent></item>
<item><cod>P01</cod><isparent>N</isparent></item>
<item><cod>P02</cod><isparent>N</isparent></item>
<item><cod>P03</cod><isparent>N</isparent></item>
<item><cod>A01</cod><isparent>Y</isparent></item>
<item><cod>P04</cod><isparent>N</isparent></item>
<stru><parent>B01</parent><child>B11</child></stru>
<stru><parent>B01</parent><child>B12</child></stru>
<stru><parent>B11</parent><child>B21</child></stru>
</list>


The needed result is shown below. I don't know how to do this.


I found a transformation that creates that structure if the root node is known.



<list>
<itemlist>
<item>
<cod>A01</cod>
<itemlist>
<item>
<cod>P04</cod>
</item>
</itemlist>
</item>
<item>
<cod>B01</cod>
<itemlist>
<item>
<cod>B11</cod>
<itemlist>
<item>
<cod>B21</cod>
<itemlist>
<item>
<cod>P02</cod>
</item>
<item>
<cod>P03</cod>
</item>
</itemlist>
</item>
<item>
<cod>P01</cod>
</item>
</itemlist>
</item>
<item>
<cod>B12</cod>
<itemlist>
<item>
<cod>P01</cod>
</item>
<item>
<cod>P02</cod>
</item>
</itemlist>
</item>
</itemlist>
</item>
</itemlist>
</list>

No comments:

Post a Comment