xsl-list
[Top] [All Lists]

Re: [xsl] Help XSL Transformation of Data

2009-09-28 11:25:35
Hi Martin


I am using the code snippet you provided, along with the row to output the 
time. But this doesn't seem to work. 
The only thing I changed was to remove the template definition and added 
Chart-Data before Root. 

I get an error XML Parsing Error: no element found

I am complete newbie, so please let me know if I have to use template 
definition and how to use it. 




  <xsl:key name="k1" match="Metrics/*" use="local-name()"/>


    <chart_data>
<!--  Category (X axis) date row -->
<row>
  <null/>

  <xsl:for-each select="Chart-Data/Root/Item[position()=1]/Statistics/Item">
<string><xsl:value-of select="@StartRawDateAndTime"/></string>
  </xsl:for-each>
  
</row>
      <xsl:for-each select="Chart-Data/Root/Item/Statistics/Item[1]/Metrics/*">
        <row>
          <string><xsl:value-of select="local-name()"/></string>
          <xsl:for-each select="key('k1', local-name())">
            <number><xsl:value-of select="number(@TotalValue)"/></number>
          </xsl:for-each>
        </row>
      </xsl:for-each>
    </chart_data>

  





----- Original Message ----
From: Martin Honnen <Martin(_dot_)Honnen(_at_)gmx(_dot_)de>
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Sent: Saturday, September 26, 2009 6:28:57 AM
Subject: Re: [xsl] Help XSL Transformation of Data

Rajesh Jain wrote:

I need help to transform the XML data to plot a graph. 
The metrics data has to be pulled outside and organized by Time. 
The Metric Counter names (CPU, Disk, Memory) are dynamic, I mean, I could get 
five metrics next time.

Please tell me how to pull data by the Names and organize by Time.





<Chart-Data> <Root>   <Item ID="Server">    <Statistics >         <Item 
Time="1253419200000">           <Metrics>              <CPU 
TotalValue="20.0"/ >              <Disk TotalValue="10.0" / >              
<Memory TotalValue="20.0" / >           </Metrics >         </Item >         
<Item Time="1253419211000">           <Metrics>              <CPU 
TotalValue="40.0"/ >              <Disk TotalValue="60.0" / >              
<Memory TotalValue="10.0" / >           </Metrics >         </Item >     
</Statistics>   </Item> </Root> <Chart-Data> 
This has to be Transformed in this XSL output 
<chart_data>                 <row>                         <null/>            
             <string>10</string>                         <string>11</string>  
               </row> 

I don't understand where above 'row' comes from.

                <row>                         <string>CPU</string>            
             <number >20</number>                         <number 
40</number>                 </row>                 <row>                     
    <string>Memory</string>                         <number>20</number>       
                  <number >10</number>                 </row>                 
<row>                         <string>Disk</string>                         
<number >10</number>                         <number>60</number>              
   </row> </chart_data>

And I don't see where data is organized by time. So based on that all I can 
suggest is


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

  <xsl:output indent="yes"/>

  <xsl:key name="k1" match="Metrics/*" use="local-name()"/>

  <xsl:template match="Chart-Data">
    <chart_data>
      <xsl:for-each select="Root/Item/Statistics/Item[1]/Metrics/*">
        <row>
          <string><xsl:value-of select="local-name()"/></string>
          <xsl:for-each select="key('k1', local-name())">
            <number><xsl:value-of select="number(@TotalValue)"/></number>
          </xsl:for-each>
        </row>
      </xsl:for-each>
    </chart_data>
  </xsl:template>

</xsl:stylesheet>

which outputs

<chart_data>
   <row>
      <string>CPU</string>
      <number>20</number>
      <number>40</number>
   </row>
   <row>
      <string>Disk</string>
      <number>10</number>
      <number>60</number>
   </row>
   <row>
      <string>Memory</string>
      <number>20</number>
      <number>10</number>
   </row>
</chart_data>

which is what you described above, with the exception of the first 'row' 
element in your described output, as I can't relate that to input data.


-- 
    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


      

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--