【问题标题】:XSL transformation - XML data to HTML table-horizontalXSL 转换 - XML 数据到 HTML 表格水平
【发布时间】:2018-03-25 12:11:59
【问题描述】:

我尝试在这个问题XSL transformation - XML data to HTML table 中使用 XSLT 2.0 做同一张表,但我想要这样的输出 - 标题将在左侧。

有没有办法为此编写样式表?

XML

<?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>

【问题讨论】:

    标签: xml xslt-2.0


    【解决方案1】:

    你只需要创建相应的HTML表格结构:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        expand-text="yes"
        version="3.0">
    
      <xsl:output method="html" indent="yes" html-version="5"/>
    
      <xsl:template match="/">
        <html>
          <head>
            <title>.NET XSLT Fiddle Example</title>
            <style xsl:expand-text="no">
                table {
                  border-collapse: collapse; width: 100%;
                }
                th, td {
                    border-bottom: 1px solid #ddd;
                }
            </style>
          </head>
          <body>
              <xsl:apply-templates/>
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="root">
          <table>
              <tr>
                  <td>Time</td>
                  <xsl:for-each select="sort(distinct-values(sample/@time))">
                      <td>{.}</td>
                  </xsl:for-each>
              </tr>
              <xsl:for-each-group select="sample[starts-with(@label, 'cpu')]" group-by="@label">
                  <xsl:sort select="current-grouping-key()"/>
                  <tr>
                      <td>{current-grouping-key()}</td>
                      <xsl:for-each select="current-group()">
                          <xsl:sort select="@time"/>
                          <td>{value}</td>
                      </xsl:for-each>
                  </tr>
              </xsl:for-each-group>
          </table>
      </xsl:template>
    
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/3Nqn5Yp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多