XSL转换 - 将XML数据转换为HTML表格 [英] XSL transformation - XML data to HTML table
问题描述
有没有办法如何为这个类型创建一个XSL样式表,在下面的图片上输出一个像HTML表格的XML文档?问题是,生成的数据随机存储在XML文档中,所以我不知道如何为它编写样式表。你可以帮我吗?
非常感谢。
这是一个带有随机排序数据的XML文档(内存,CPU0,CPU1,CPU2)。这个数据有两个时间(time = 1和time = 14)。
<?xml version =1.0encoding = UTF-8 >?;
< root>
< sample time =14label =cpu_0>
<值> 22< /值>
< / sample>
< sample time =14label =cpu_2>
< value> 6< /值>
< / sample>
< sample time =1label =cpu_2>
<值> 4< /值>
< / sample>
< sample time =14label =memory>
<值> 97< /值>
< / sample>
< sample time =1label =cpu_0>
<值> 28< /值>
< / sample>
< sample time =14label =cpu_1>
<值> 52< /值>
< / sample>
< sample time =1label =memory>
<值> 55< /值>
< / sample>
< sample time =1label =cpu_1>
<值> 21< /值>
< / sample>
< / root>
这是需要的HTML表格输出。
您应该可以通过以下方式轻松完成此操作:
- 通过
track
进行分组(使用xsl:for-每个组
)。 - 跟踪需要输出的列。
示例...
XSLT 2.0
< xsl:stylesheet version =2.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
<! - 跟踪所有的CPU列。 - >
< xsl:variable name =colsas =item()*>
select =distinct-values(// sample [starts-with(@ label,'cpu')] / @ label)/>
< xsl:perform-sort select =$ init>
< xsl:sort order =ascendingdata-type =text/>
< / xsl:perform-sort>
< / xsl:variable>
< xsl:template match =/ *>
< html>
< body>
< table>
< thead>
< tr>
< th>时间< / th>
< xsl:for-each select =$ cols>
< th>< xsl:value-of select =upper-case(replace(。,'_',''))/> (%)< /第>
< / xsl:for-each>
< / tr>
< / thead>
< tbody>
< xsl:sort select =@ timeorder =ascendingdata-type =number/>
< tr>
< td>
< xsl:value-of select =current-grouping-key()/>
< / td>
< xsl:for-each select =$ cols>
< td>
< / td>
< / xsl:for-each>
< / tr>
< / xsl:for-each-group>
< / tbody>
< / table>
< / body>
< / html>
< / xsl:template>
< / xsl:stylesheet>
输出
<
< html> <身体GT; <表> < THEAD> < TR> <的第i;时间< /第> < th> CPU 0(%)< / th> CPU 1(%)< / th> CPU 2(%)< / th> < / TR> < / THEAD> < TBODY> < TR> < TD→1< / TD> < TD> 28和60; / TD> < TD> 21< / TD> < TD> 4℃; / TD> < / TR> < TR> < TD> 14< / TD> < TD> 22℃; / TD> < TD> 52< / TD> < TD→6< / TD> < / TR> < / tbody的> < /表> < / body>< / html>
Is there way how to create a XSL stylesheet for this type a XML document for output like HTML table on the picture below? The problem is, that generated datas are stored randomly in XML document, so I do not know how to write a stylesheet for it. Can you help me please?
Thank you a lot.
This is a XML document with random sort datas (Memory, CPU0, CPU1, CPU2). This data has two time (time=1 and time=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>
This is the wanted HTML table output.
You should be able to do this pretty easily by:
- Grouping by
track
(usingxsl:for-each-group
). - Keeping track of what columns need to be output.
Example...
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>
Output
<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>
See fiddle here: http://xsltfiddle.liberty-development.net/eiQZDbp
这篇关于XSL转换 - 将XML数据转换为HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!