Subject: Re: [xsl] Reordering a complex hierarchical structure From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx> Date: Wed, 30 Aug 2006 21:28:39 +0530 |
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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>
<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>
<?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>
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Reordering a complex hier, David Carlisle | Thread | [xsl] Re: Reordering a complex hier, Ian Wootten |
Re: [xsl] XPath location problem, Per Åberg | Date | [xsl] Optimization of style sheets, meenakshi n |
Month |