xsl-list
[Top] [All Lists]

flat structure to deep structure in a smart way

2004-09-24 10:00:01

I need to transform a flat structure (from a database) to a deep
structure. The flat structure is:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <line count="1">
    <item itemcount="1">A1</item>
    <item itemcount="2">B1</item>
    <item itemcount="3">C1</item>
    <item itemcount="4">D1</item>
    <item itemcount="5">E1</item>
  </line>
  <line count="2">
    <item itemcount="1">A1</item>
    <item itemcount="2">B22</item>
    <item itemcount="3">C22</item>
    <item itemcount="4">D22</item>
    <item itemcount="5">E22</item>
  </line>
  <line count="3">
    <item itemcount="1">A3</item>
    <item itemcount="2">B3</item>
    <item itemcount="3">C3</item>
    <item itemcount="4">D3</item>
    <item itemcount="5">E3</item>
  </line>
  <line count="4">
    <item itemcount="1">A3</item>
    <item itemcount="2">B3</item>
    <item itemcount="3">C4</item>
    <item itemcount="4">D4</item>
    <item itemcount="5">E4</item>
  </line>
  <line count="5">
    <item itemcount="1">A3</item>
    <item itemcount="2">B3</item>
    <item itemcount="3">C4</item>
    <item itemcount="4">D5</item>
    <item itemcount="5">E5</item>
  </line>
</root>

In the element <line count="2"> you see an A1 again, so B22 should fall
under A1 that's under line 1. I.e. if an element has the same value as
in the previous line, it should become a subelement of the first line.

So the output should look like:

<?xml version="1.0" encoding="UTF-8"?>
<Result>
  <A Value="A1"><!-- from line count=1 -->
    <B Value="B1">
      <C Value="C1">
        <D Value="D1">
          <E Value="E1"/>
        </D>
      </C>  
    </B>
    <B Value="B22"><!-- from line count=2, comes under line 1 as A1 is
repeated -->
      <C Value="C22">
        <D Value="D22">
          <E Value="E22"/>
        </D>
      </C>
    </B>
  </A>
  <A Value="A3"><!-- from line count=3 -->
    <B Value="B3">
      <C Value="C3">
        <D Value="D3">
          <E Value="E3"/>
        </D>
      </C>
      <C Value="C4"><!-- from line count=4 -->
        <D Value="D4">
          <E Value="E4"/>
        </D>
        <D Value="D5"><!-- from line count=5 -->
          <E Value="E5"/>
        </D>
      </C>
    </B>
  </A>
</Result>

What is the best strategy to accomplish this ?

Many thanks in advance.

Jozef


<Prev in Thread] Current Thread [Next in Thread>