XSL 转换 - XML 数据到不同的 HTML 表 [英] XSL transformation - XML data to different HTML tables

查看:30
本文介绍了XSL 转换 - XML 数据到不同的 HTML 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,例如,此 XML 代码的 XSL 样式表如何?我根据这个问题(下面)尝试了一些东西,但我的输出在下面看起来不像我想要的.XSL 转换 - XML 数据到 HTML 表格

Hello how would looks XSL stylesheet for example for this XML code? I tried something according to this question (below) but my output did not look as I want below. XSL transformation - XML data to HTML table

XML 代码

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <sample time="14" label="Test1:cpu_2:usage_high">
        <value>96</value>
    </sample>
    <sample time="14" label="Test1:cpu_2:usage_low">
        <value>1</value>
    </sample>
    <sample time="1" label="Test1:cpu_1:usage_high">
        <value>97</value>
    </sample>
    <sample time="1" label="Test1:cpu_2:usage_low">
        <value>7</value>
    </sample>
    <sample time="1" label="Test1:cpu_1:usage_low">
        <value>6</value>
    </sample>
    <sample time="14" label="Test1:cpu_1:usage_low">
        <value>11</value>
    </sample>
    <sample time="1" label="Test1:cpu_2:usage_high">
        <value>91</value>
    </sample>
    <sample time="14" label="Test1:cpu_1:usage_high">
        <value>89</value>
    </sample>
</root>

HTML 代码

<html>
   <body>
      <h2>CPU1</h2>
          <table>
             <thead>
                <tr>
                   <th>Time</th>
                   <th>High</th>
                   <th>Low</th>
                </tr>
             </thead>
             <tbody>
                <tr>
                   <td>1</td>
                   <td>97</td>
                   <td>6</td>
                </tr>
                <tr>
                   <td>14</td>
                   <td>89</td>
                   <td>11</td>
                </tr>
             </tbody>
          </table>
        <h2>CPU2</h2>
          <table>
             <thead>
                <tr>
                   <th>Time</th>
                   <th>High</th>
                   <th>Low</th>
                </tr>
             </thead>
             <tbody>
                <tr>
                   <td>1</td>
                   <td>91</td>
                   <td>7</td>
                </tr>
                <tr>
                   <td>14</td>
                   <td>96</td>
                   <td>1</td>
                </tr>
             </tbody>
          </table>  
   </body>
</html>

推荐答案

您应该首先按 cpu 编号(标记化时 @label 中的第二个标记)对 sample 元素进行分组by :).

You should first group the sample elements by the cpu number (second token in @label when tokenized by :).

然后按@time分组得到每一行.

Then group by @time to get each of the rows.

示例...

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="*"/>

  <xsl:template match="/*">
    <html>
      <body>
        <xsl:for-each-group select="sample" group-by="tokenize(@label, ':')[2]">
          <xsl:sort select="current-grouping-key()"/>
          <h2>
            <xsl:value-of select="upper-case(replace(current-grouping-key(), '_', ''))"/>
          </h2>
          <table>
            <thead>
              <tr>
                <th>Time</th>
                <th>High</th>
                <th>Low</th>
              </tr>
            </thead>
            <tbody>
              <xsl:for-each-group select="current-group()" group-by="@time">
                <xsl:sort select="current-grouping-key()"/>
                <tr>
                  <td>
                    <xsl:value-of select="current-grouping-key()"/>
                  </td>
                  <td>
                    <xsl:value-of select="current-group()[ends-with(@label, 'high')]/value"/>
                  </td>
                  <td>
                    <xsl:value-of select="current-group()[ends-with(@label, 'low')]/value"/>
                  </td>
                </tr>
              </xsl:for-each-group>
            </tbody>
          </table>
        </xsl:for-each-group>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

输出

<html>
   <body>
      <h2>CPU1</h2>
      <table>
         <thead>
            <tr>
               <th>Time</th>
               <th>High</th>
               <th>Low</th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>1</td>
               <td>97</td>
               <td>6</td>
            </tr>
            <tr>
               <td>14</td>
               <td>89</td>
               <td>11</td>
            </tr>
         </tbody>
      </table>
      <h2>CPU2</h2>
      <table>
         <thead>
            <tr>
               <th>Time</th>
               <th>High</th>
               <th>Low</th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>1</td>
               <td>91</td>
               <td>7</td>
            </tr>
            <tr>
               <td>14</td>
               <td>96</td>
               <td>1</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

可以在这里看到一个工作示例:http://xsltfiddle.liberty-development.net/eiQZDbq

A working example can be seen here: http://xsltfiddle.liberty-development.net/eiQZDbq

这是另一个更通用的选项.它只处理具有第三个标记的标签属性的样本...

Here's another option that is more generic. It only processes samples with a label attribute that has a 3rd token...

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

  <xsl:function name="local:format">
    <xsl:param name="str"/>
    <xsl:param name="delim"/>
    <xsl:variable name="tokens" as="item()*">
      <xsl:for-each select="tokenize($str,$delim)">
        <xsl:sequence select="concat(upper-case(substring(.,1,1)),substring(.,2))"/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select="string-join($tokens, ' ')"/>
  </xsl:function>

  <xsl:template match="/*">
    <html>
      <body>
        <xsl:for-each-group select="sample[tokenize(@label,':')[3]]" group-by="tokenize(@label, ':')[2]">
          <xsl:sort select="current-grouping-key()"/>
          <xsl:variable name="cols" select="distinct-values(current-group()/@label/tokenize(.,':')[3])" as="item()*"/>
          <h2>
            <xsl:value-of select="upper-case(replace(current-grouping-key(), '_', ''))"/>
          </h2>
          <table>
            <thead>
              <tr>
                <th>Time</th>
                <xsl:for-each select="$cols">
                  <th>
                    <xsl:value-of select="local:format(.,'_')"/>
                  </th>
                </xsl:for-each>
              </tr>
            </thead>
            <tbody>
              <xsl:for-each-group select="current-group()" group-by="@time">
                <xsl:sort select="current-grouping-key()"/>
                <tr>
                  <td>
                    <xsl:value-of select="current-grouping-key()"/>
                  </td>
                  <xsl:for-each select="$cols">
                    <td>
                      <xsl:value-of select="current-group()[ends-with(@label, current())]/value"/>
                    </td>
                  </xsl:for-each>
                </tr>
              </xsl:for-each-group>
            </tbody>
          </table>
        </xsl:for-each-group>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

这篇关于XSL 转换 - XML 数据到不同的 HTML 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆