你好怎么会看起来XSL样式表例如,对于这个XML代码? 根据我对这个问题(下)尝试的东西,但我的输出没有看,因为我想在下面。 XSL转换- XML数据的HTML表
XML代码
<?xml version="1.0" encoding="utf-8" ?>
<root>
<sample time="14" label="Test1:cpu_2:usage_high">
<value>96</value>
</sample>
<sample time="14" label="Test1:cpu_2:usage_low">
<value>1</value>
</sample>
<sample time="1" label="Test1:cpu_1:usage_high">
<value>97</value>
</sample>
<sample time="1" label="Test1:cpu_2:usage_low">
<value>7</value>
</sample>
<sample time="1" label="Test1:cpu_1:usage_low">
<value>6</value>
</sample>
<sample time="14" label="Test1:cpu_1:usage_low">
<value>11</value>
</sample>
<sample time="1" label="Test1:cpu_2:usage_high">
<value>91</value>
</sample>
<sample time="14" label="Test1:cpu_1:usage_high">
<value>89</value>
</sample>
</root>
HTML代码
<html>
<body>
<h2>CPU1</h2>
<table>
<thead>
<tr>
<th>Time</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>97</td>
<td>6</td>
</tr>
<tr>
<td>14</td>
<td>89</td>
<td>11</td>
</tr>
</tbody>
</table>
<h2>CPU2</h2>
<table>
<thead>
<tr>
<th>Time</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>91</td>
<td>7</td>
</tr>
<tr>
<td>14</td>
<td>96</td>
<td>1</td>
</tr>
</tbody>
</table>
</body>
</html>
应该第一组sample
由CPU数目的元素(在第二令牌@label
当由记号化:
)。
然后按组@time
得到每一行。
例...
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="*"/>
<xsl:template match="/*">
<html>
<body>
<xsl:for-each-group select="sample" group-by="tokenize(@label, ':')[2]">
<xsl:sort select="current-grouping-key()"/>
<h2>
<xsl:value-of select="upper-case(replace(current-grouping-key(), '_', ''))"/>
</h2>
<table>
<thead>
<tr>
<th>Time</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<xsl:for-each-group select="current-group()" group-by="@time">
<xsl:sort select="current-grouping-key()"/>
<tr>
<td>
<xsl:value-of select="current-grouping-key()"/>
</td>
<td>
<xsl:value-of select="current-group()[ends-with(@label, 'high')]/value"/>
</td>
<td>
<xsl:value-of select="current-group()[ends-with(@label, 'low')]/value"/>
</td>
</tr>
</xsl:for-each-group>
</tbody>
</table>
</xsl:for-each-group>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
产量
<html> <body> <h2>CPU1</h2> <table> <thead> <tr> <th>Time</th> <th>High</th> <th>Low</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>97</td> <td>6</td> </tr> <tr> <td>14</td> <td>89</td> <td>11</td> </tr> </tbody> </table> <h2>CPU2</h2> <table> <thead> <tr> <th>Time</th> <th>High</th> <th>Low</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>91</td> <td>7</td> </tr> <tr> <td>14</td> <td>96</td> <td>1</td> </tr> </tbody> </table> </body> </html>
工作示例可以在这里看到: http://xsltfiddle.liberty-development.net/eiQZDbq
这里的另一个选项是更通用。 它仅处理样品,有一个第三个符号标签属性...
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:local="local">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:function name="local:format">
<xsl:param name="str"/>
<xsl:param name="delim"/>
<xsl:variable name="tokens" as="item()*">
<xsl:for-each select="tokenize($str,$delim)">
<xsl:sequence select="concat(upper-case(substring(.,1,1)),substring(.,2))"/>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="string-join($tokens, ' ')"/>
</xsl:function>
<xsl:template match="/*">
<html>
<body>
<xsl:for-each-group select="sample[tokenize(@label,':')[3]]" group-by="tokenize(@label, ':')[2]">
<xsl:sort select="current-grouping-key()"/>
<xsl:variable name="cols" select="distinct-values(current-group()/@label/tokenize(.,':')[3])" as="item()*"/>
<h2>
<xsl:value-of select="upper-case(replace(current-grouping-key(), '_', ''))"/>
</h2>
<table>
<thead>
<tr>
<th>Time</th>
<xsl:for-each select="$cols">
<th>
<xsl:value-of select="local:format(.,'_')"/>
</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each-group select="current-group()" group-by="@time">
<xsl:sort select="current-grouping-key()"/>
<tr>
<td>
<xsl:value-of select="current-grouping-key()"/>
</td>
<xsl:for-each select="$cols">
<td>
<xsl:value-of select="current-group()[ends-with(@label, current())]/value"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each-group>
</tbody>
</table>
</xsl:for-each-group>
</body>
</html>
</xsl:template>
</xsl:stylesheet>