You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用XSLT 2.0编写XML转表头居左的HTML横向表格样式表?

实现左侧表头的XSLT 2.0转换方案

当然可以搞定!我们可以借助XSLT 2.0的分组和节点处理能力,把你提供的XML转换成左侧带表头的HTML表格,具体实现方案如下:

完整XSLT样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" encoding="UTF-8"/>

    <!-- 收集所有唯一的时间值并排序 -->
    <xsl:variable name="unique-times" select="distinct-values(/root/sample/@time)" as="xs:string*">
        <xsl:sort select="." data-type="number"/>
    </xsl:variable>

    <!-- 收集所有唯一的标签值并排序 -->
    <xsl:variable name="unique-labels" select="distinct-values(/root/sample/@label)" as="xs:string*">
        <xsl:sort select="."/>
    </xsl:variable>

    <xsl:template match="/">
        <html>
            <body>
                <table border="1">
                    <!-- 第一行:空表头单元格 + 各时间点作为列标题 -->
                    <tr>
                        <th></th>
                        <xsl:for-each select="$unique-times">
                            <th><xsl:value-of select="."/></th>
                        </xsl:for-each>
                    </tr>

                    <!-- Time行:左侧表头 + 对应时间值 -->
                    <tr>
                        <th>Time</th>
                        <xsl:for-each select="$unique-times">
                            <td><xsl:value-of select="."/></td>
                        </xsl:for-each>
                    </tr>

                    <!-- 遍历每个标签,生成左侧表头行 -->
                    <xsl:for-each select="$unique-labels">
                        <xsl:variable name="current-label" select="."/>
                        <tr>
                            <th><xsl:value-of select="$current-label"/></th>
                            <xsl:for-each select="$unique-times">
                                <xsl:variable name="current-time" select="."/>
                                <!-- 匹配对应时间和标签的数值 -->
                                <xsl:variable name="matching-value" select="/root/sample[@time = $current-time and @label = $current-label]/value"/>
                                <td>
                                    <xsl:value-of select="if ($matching-value) then $matching-value else ''"/>
                                </td>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

关键逻辑说明

  • 数据预处理:用distinct-values()函数提取XML中所有不重复的time和label值,并排序确保表格展示有序;
  • 表格结构搭建:
    • 第一行左侧留空,后续单元格填充所有唯一时间点作为列标题;
    • 第二行左侧表头为Time,对应单元格填充时间值;
    • 后续每行以label作为左侧表头,遍历每个时间点,查找匹配的<sample>元素的value值,存在则填充,否则留空。

转换后HTML效果

应用上述样式表后,你会得到如下结构的HTML表格:

<html>
   <body>
      <table border="1">
         <tr>
            <th></th>
            <th>1</th>
            <th>14</th>
         </tr>
         <tr>
            <th>Time</th>
            <td>1</td>
            <td>14</td>
         </tr>
         <tr>
            <th>cpu_0</th>
            <td></td>
            <td>22</td>
         </tr>
         <tr>
            <th>cpu_2</th>
            <td>...</td>
            <td>6</td>
         </tr>
      </table>
   </body>
</html>

这个表格的核心表头(Time、cpu_0、cpu_2)全部位于左侧第一列,完全符合你的需求。

内容的提问来源于stack exchange,提问作者J. Hradek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:10:07