有没有办法为这种类型的XML文档创建XSL样式表,以便输出如下图所示的HTML表?问题是,生成的数据随机存储在XML文档中,所以我不知道如何为它编写样式表。你能帮我吗?
非常感谢。
这是一个具有随机排序数据(内存,CPU0,CPU1,CPU2)的XML文档。此数据有两个时间(时间= 1,时间= 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>
这是所需的HTML表输出。
您应该可以通过以下方法很容易地做到这一点:
track
(使用xsl:for-each-group
)。例...
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>
输出量
<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>
在这里查看小提琴:http : //xsltfiddle.liberty-development.net/eiQZDbp
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句