[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Reordering a complex hierarchical structure

Subject: Re: Reordering a complex hierarchical structure
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Wed, 30 Aug 2006 21:28:39 +0530
metric name
Hope the stylesheet below will help:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

<xsl:key name="by-id" match="service" use="id" />

<xsl:template match="/">
  <interactions>
    <xsl:for-each select="//service[generate-id() =
generate-id(key('by-id', id)[1])]">
      <service name="{name}" id="{id}">
        <xsl:copy-of select="key('by-id', id)/../metric" />
      </service>
    </xsl:for-each>
  </interactions>
</xsl:template>

</xsl:stylesheet>

This when applied to XML:

<x>
<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>
</x>

Produces output:

<?xml version="1.0" encoding="UTF-8"?>
<interactions>
  <service name="somename" id="1">
     <metric name="somemetric">value1</metric>
     <metric name="anothermetric">value2</metric>
     <metric name="andanothermetric">value3</metric>
     <metric name="somemetric">value4</metric>
     <metric name="anothermetric">value5</metric>
     <metric name="andanothermetric">value6</metric>
  </service>
  <service name="anothername" id="2">
     <metric name="somemetric">value7</metric>
     <metric name="anothermetric">value8</metric>
     <metric name="andanothermetric">value9</metric>
     <metric name="somemetric">value10</metric>
     <metric name="anothermetric">value11</metric>
     <metric name="andanothermetric">value12</metric>
  </service>
</interactions>


On 8/30/06, Ian Wootten <I.M.Wootten@xxxxxxxxxxxxxxxx> wrote:
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


--
Regards,
Mukul Gandhi

http://gandhimukul.tripod.com

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.