[xsl] Reordering a complex hierarchical structure

Subject: [xsl] Reordering a complex hierarchical structure
From: Ian Wootten <I.M.Wootten@xxxxxxxxxxxxxxxx>
Date: Wed, 30 Aug 2006 16:10:33 +0100
I'm attempting to structure a complex xml structure to be represented in
a more meaningful manner. I'm doing this within a xml transformer in Java.

Currently the structure is like this:

<session>
   <service>
         <name>somename</name>
         <id>1</id>
   </service>
   <metric name="somemetric">value1</metric>
   <metric name="anothermetric">value2</metric>
   <metric name="andanothermetric">value3</metric>
</session>
<session>
   <service>
         <name>somename</name>
         <id>1</id>
   </service>
   <metric name="somemetric">value4</metric>
   <metric name="anothermetric">value5</metric>
   <metric name="andanothermetric">value6</metric>
</session>
<session>
   <service>
         <name>anothername</name>
         <id>2</id>
   </service>
   <metric name="somemetric">value7</metric>
   <metric name="anothermetric">value8</metric>
   <metric name="andanothermetric">value9</metric>
</session>
<session>
   <service>
         <name>anothername</name>
         <id>2</id>
   </service>
   <metric name="somemetric">value10</metric>
   <metric name="anothermetric">value11</metric>
   <metric name="andanothermetric">value12</metric>
</session>

I'd like it to be structured as follows

service  ----> id1 ---->  somemetric ----> value1
                          -----> somemetric ----> value4
                           ---->  anothermetric ----> value2
                          -----> anothermetric ----> value5
                           -----> andanothermetric ----> value3
                          -----> andanothermetric ----> value6
            ------> id 2 ---->  somemetric----> value7
                          -----> somemetric ----> value10
                           ---->  anothermetric ----> value8
                          -----> anothermetric ----> value11
                           -----> andanothermetric ----> value9
                          -----> andanothermetric ----> value12

I currently use an xsl stylesheet like the following, which basically
collects all the metrics from the document and orders them by the name
they have.

<xsl:key name = "x" match="metric" use="@name"/>

   <xsl:template match="/">
       <interactions>
           <xsl:apply-templates select="//session"/>
       </interactions>
   </xsl:template>

   <xsl:template match="session">
       <service name="{service/name}">
           <request><xsl:value-of select="service/id" /></request>
                   <xsl:apply-templates select="metric[generate-id(.) =
generate-id(key('x', @name)[1])]" />
       </service>
     </xsl:template>

   <xsl:template match="metric">
       <metric name="{@name}">
       <xsl:for-each select="key('x', @name)">
       <xsl:for-each select="value">
           <value>
           <xsl:value-of select="node()" />
             </value>
       </xsl:for-each>

       </xsl:for-each>
            </metric>
   </xsl:template>

I am struggling to determine how I can "divide" this structure up so it
appears by the identifier on each service. Once the structure has been
created in this manner the identifiers for the services which they were
under are lost. Is anyone able to help?

Many thanks,

Ian

Current Thread