我尝试使用XSLT 2.0做同样的表,就像在这个问题XSL transformation - XML data to HTML table中一样,但我希望输出像这样 - 标题将在左侧。
有没有办法为此编写样式表?
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>
答案 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>