Jakub Oškera
Jakub Oškera

Reputation: 67

XSL transformation - XML data to HTML table

Is there way how to create a XSL stylesheet for this type a XML document for output like HTML table on the picture below? The problem is, that generated datas are stored randomly in XML document, so I do not know how to write a stylesheet for it. Can you help me please?

Thank you a lot.

This is a XML document with random sort datas (Memory, CPU0, CPU1, CPU2). This data has two time (time=1 and time=14).

 <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <sample time="14" label="cpu_0">
            <value>22</value>
        </sample>
        <sample time="14" label="cpu_2">
            <value>6</value>
        </sample>
        <sample time="1" label="cpu_2">
            <value>4</value>
        </sample>
        <sample time="14" label="memory">
            <value>97</value>
        </sample>
        <sample time="1" label="cpu_0">
            <value>28</value>
        </sample>
        <sample time="14" label="cpu_1">
            <value>52</value>
        </sample>
        <sample time="1" label="memory">
            <value>55</value>
        </sample>
        <sample time="1" label="cpu_1">
            <value>21</value>
        </sample>
    </root>

This is the wanted HTML table output.

Wanted output HTML table

Upvotes: 0

Views: 367

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52888

You should be able to do this pretty easily by:

  1. Grouping by track (using xsl:for-each-group).
  2. Keeping track of what columns need to be output.

Example...

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!--Keep track of all CPU columns.-->
  <xsl:variable name="cols" as="item()*">
    <xsl:variable name="init" 
      select="distinct-values(//sample[starts-with(@label,'cpu')]/@label)"/>
    <xsl:perform-sort select="$init">
      <xsl:sort order="ascending" data-type="text"/>
    </xsl:perform-sort>
  </xsl:variable>

  <xsl:template match="/*">
    <html>
      <body>
        <table>
          <thead>
            <tr>
              <th>Time</th>
              <xsl:for-each select="$cols">
                <th><xsl:value-of select="upper-case(replace(.,'_',' '))"/> (%)</th>
              </xsl:for-each>             
            </tr>
          </thead>
          <tbody>
            <xsl:for-each-group select="sample" group-by="@time">
              <xsl:sort select="@time" order="ascending" data-type="number"/>
              <tr>
                <td>
                  <xsl:value-of select="current-grouping-key()"/>
                </td>
                <xsl:for-each select="$cols">
                  <td>
                    <xsl:value-of select="current-group()[@label=current()]"/>
                  </td>
                </xsl:for-each>
              </tr>
            </xsl:for-each-group>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

Output

<html>
   <body>
      <table>
         <thead>
            <tr>
               <th>Time</th>
               <th>CPU 0 (%)</th>
               <th>CPU 1 (%)</th>
               <th>CPU 2 (%)</th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>1</td>
               <td>28</td>
               <td>21</td>
               <td>4</td>
            </tr>
            <tr>
               <td>14</td>
               <td>22</td>
               <td>52</td>
               <td>6</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

See fiddle here: http://xsltfiddle.liberty-development.net/eiQZDbp

Upvotes: 1

Related Questions