使用XSLT 2或3,可以使用嵌套的
for-each-group
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
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>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Results">
<table>
<xsl:variable name="servers" select="Data/ServerName"/>
<thead>
<tr>
<th>Key</th>
<xsl:for-each select="$servers">
<th>{.}</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each-group select=".//Category" group-by="string(@Name)">
<xsl:variable name="cat-group" select="current-group()"/>
<tr>
<th>{current-grouping-key()}</th>
<th>{$servers[1]}</th>
<th>{$servers[2]}</th>
</tr>
<xsl:for-each-group select="current-group()" group-by="Parameters/Parameter/@key">
<tr>
<td>{current-grouping-key()}</td>
<td>{($cat-group[ancestor::Data[ServerName = $servers[1]]]/Parameters/Parameter[@key = current-grouping-key()]/@value, 'N/A')[1]}</td>
<td>{($cat-group[ancestor::Data[ServerName = $servers[2]]]/Parameters/Parameter[@key = current-grouping-key()]/@value, 'N/A')[1]}</td>
</tr>
</xsl:for-each-group>
</xsl:for-each-group>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/6qM2e2i/3
给出输出
<table>
<thead>
<tr>
<th>Key</th>
<th>Server1</th>
<th>Server2</th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th>Server1</th>
<th>Server2</th>
</tr>
<tr>
<td>A</td>
<td>1</td>
<td>10</td>
</tr>
<tr>
<td>B</td>
<td>2</td>
<td>20</td>
</tr>
<tr>
<td>C</td>
<td>3</td>
<td>N/A</td>
</tr>
<tr>
<td>D</td>
<td>N/A</td>
<td>30</td>
</tr>
<tr>
<th>Misc</th>
<th>Server1</th>
<th>Server2</th>
</tr>
<tr>
<td>A</td>
<td>2</td>
<td>N/A</td>
</tr>
<tr>
<td>B</td>
<td>5</td>
<td>N/A</td>
</tr>
<tr>
<td>C</td>
<td>N/A</td>
<td>2</td>
</tr>
<tr>
<td>D</td>
<td>N/A</td>
<td>5</td>
</tr>
</tbody>
</table>
至于服务器列的动态数量,我希望
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
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>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Results">
<table>
<xsl:variable name="servers" select="Data/ServerName"/>
<thead>
<tr>
<th>Key</th>
<xsl:for-each select="$servers">
<th>{.}</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each-group select=".//Category" group-by="string(@Name)">
<xsl:variable name="cat-group" select="current-group()"/>
<tr>
<th>{current-grouping-key()}</th>
<xsl:for-each select="$servers">
<th>{.}</th>
</xsl:for-each>
</tr>
<xsl:for-each-group select="current-group()" group-by="Parameters/Parameter/@key">
<tr>
<td>{current-grouping-key()}</td>
<xsl:for-each select="$servers">
<td>{($cat-group[ancestor::Data[ServerName = current()]]/Parameters/Parameter[@key = current-grouping-key()]/@value, 'N/A')[1]}</td>
</xsl:for-each>
</tr>
</xsl:for-each-group>
</xsl:for-each-group>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
管理:
https://xsltfiddle.liberty-development.net/6qM2e2i/4