[xsl] Converting a hierarchical to a pseudo relational xml format -or - adding to the middle of a result tree

Subject: [xsl] Converting a hierarchical to a pseudo relational xml format -or - adding to the middle of a result tree
From: "Hugh Dixon" <hugh.dixon@xxxxxxxxxxxxxx>
Date: Sat, 22 Mar 2003 17:38:33 +1100
I am wanting to convert this:

  <chain>
    <line>
      <coord val="1 2"/>
      <coord val="3 4"/>
    </line>
   <line>
      <coord val="3 4"/>
      <coord val="5 6"/>
      <coord val="7 8"/>
    </line>
  </chain>
  <coord val="9 10"/>
  <coord val="11 12 13 14 15 16"/>
  <line>
    <coord val="17 18"/>
    <coord val="19 20 21 22 23 24"/>
  </line>


Into this:

<Coords>
  <Coord num = "1" val ="1 2"/>
  <Coord num = "2" val ="3 4"/>
  <Coord num = "3" val ="5 6"/>
  <Coord num = "4" val ="7 8"/>
  <Coord num = "5" val ="9 10"/>
  <Coord num = "6" val ="11 12"/>
  <Coord num = "7" val ="13 14"/>
  <Coord num = "8" val ="15 16"/>
  <Coord num = "9" val ="17 18"/>
  <Coord num = "10" val ="19 20"/>
  <Coord num = "11" val ="21 22"/>
  <Coord num = "12" val ="23 24"/>
</Coords>
<Lines>
  <Line num="1" coordVals="1 2"/>
  <Line num="2" coordVals="2 3 4"/>
  <Line num="3" coordVals="9 10 11 12"/>
</Lines>
<Chains>
  <Chain num="1" lineVals="1 2"/>
</Chains>

Sorry it's so long......
The number in coord::@val can be any floating point value.  The @num
values in the output document, are integers, that are generated through
the style sheet.  They are effectively primary keys, which I would like
to keep small, although this is not essential.
In the result each coord, line and chain is given a unique number, and a
list of values to the constituent elements (i.e. Chains to Lines, and
Lines to Coords)

To perform this transformation, I'd like to able to walk the source
tree, writing Coord, Line and Chain elements as required, also doing
counts to get my next number.  Is this possible?  I suspect not.
My other idea was that I write all the Coords first (as I understand you
should with XSLT!), but leave an identifier(s) on each coord node as I
process it, from which I could retrieve the Coord:@num values when the
<Line> elements are written, and similarly for the next level up.
I cannot see how to do either of these easily.
Using the first of the two ideas, I could write my output into a large
string/variable, with which I am not restricted to writing to the end.
As the output files may end up being hundreds of megabytes in size, I am
not sure this is really practical.
Could someone please offer me an alternative solution?

Thanks,
Hugh Dixon

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


Current Thread