[xsl] Tricky transformation task

Subject: [xsl] Tricky transformation task
From: Jimmy <jimmy@xxxxxxxxxxxxxx>
Date: Wed, 11 Jul 2007 16:41:57 +0200
I have the following XML structure which shows me links between objects (O1/O2).

<LINKS>
     <LINK ID="2" O1-ID="1" O2-ID="3"/>
     <LINK ID="10" O1-ID="1" O2-ID="11"/>
     <LINK ID="14" O1-ID="11" O2-ID="15"/>
     <LINK ID="18" O1-ID="2" O2-ID="4"/>
     <LINK ID="22" O1-ID="2" O2-ID="8"/>
     <LINK ID="26" O1-ID="8" O2-ID="17"/>
       ...
</LINKS>

I need to transform this into another XML structure showing me witch objects (O1/O2) and links (ID) are in the same subgraph. So I have to add something like another ID identifying which of the objects and links belong to the same graph (SUBG-ID).

this is the structure I would like to have:

<SUBG-ITEMS>
        <ITEM SUBG-ID="0" ITEM-ID="1" ITEM-TYPE="O" NAME="Object"/>
        <ITEM SUBG-ID="0" ITEM-ID="3" ITEM-TYPE="O" NAME="Object"/>
        <ITEM SUBG-ID="0" ITEM-ID="11" ITEM-TYPE="O" NAME="Object"/>
        <ITEM SUBG-ID="0" ITEM-ID="15" ITEM-TYPE="O" NAME="Object"/>
        <ITEM SUBG-ID="0" ITEM-ID="2" ITEM-TYPE="L" NAME="Link"/>
        <ITEM SUBG-ID="0" ITEM-ID="10" ITEM-TYPE="L" NAME="Link"/>
        <ITEM SUBG-ID="0" ITEM-ID="14" ITEM-TYPE="L" NAME="Link"/>

        <ITEM SUBG-ID="1" ITEM-ID="18" ITEM-TYPE="O" NAME="Object"/>
        <ITEM SUBG-ID="1" ITEM-ID="2" ITEM-TYPE="O" NAME="Object"/>
         ...
</SUBG-ITEMS>

I have thought about different ways on how to achieve this but they seem way too difficult or too time consuming at runtime. I need an easy way to give out ID's, how could I do this besides counting or maybe use the position() inside for-each? And I thought about checking previous links to see if an object is already present, if so the same SUBG-ID (count?) would have to be used.

Any hints on how to optimize this task would be very helpful. (XSL1 or 2)

Cheers
Jimmy

Current Thread