有没有function/way转置table行列/?
Is there a function/way to transpose table rows and columns/?
我希望动态转置 table 以填充 column-wise,而不是 row-wise。我的 table headers 必须先 column-wise,我的后续数据必须按该方向填充它。
为了简单起见,我创建了一个示例。
我目前的table如下:
Simple Table
不过,我希望它看起来像:
First Name| Graham | Albert | Thomas
Last Name | Bell |Einstein| Edison
Nickname| Garry | Ally | Eddy
我的XML是:
<?xml-stylesheet type = "text/xsl" href = "student.xsl"?>
<class>
<student>
<firstname>Graham</firstname>
<lastname>Bell</lastname>
<nickname>Garry</nickname>
</student>
<student>
<firstname>Albert</firstname>
<lastname>Einstein</lastname>
<nickname>Ally</nickname>
</student>
<student>
<firstname>Thomas</firstname>
<lastname>Edison</lastname>
<nickname>Eddy</nickname>
</student>
</class>
我的 XSL 是:
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/class">
<html>
<body>
<h2>Student List</h2>
<table border = "1">
<tr bgcolor="lightgreen">
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
</tr>
<xsl:for-each select = "student">
<tr>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
提前致谢!
对于通用转置,您可以执行以下操作:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/class">
<table border="1">
<xsl:for-each select="student[1]/*">
<xsl:variable name="i" select="position()"/>
<tr>
<th>
<xsl:value-of select="name()"/>
</th>
<xsl:for-each select="../../student">
<td>
<xsl:value-of select="*[$i]"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
获得:
如果您想拥有自己的标签,则需要对 table 行进行硬编码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/class">
<table border="1">
<tr>
<th>First Name</th>
<xsl:for-each select="student">
<td>
<xsl:value-of select="firstname"/>
</td>
</xsl:for-each>
</tr>
<tr>
<th>Last Name</th>
<xsl:for-each select="student">
<td>
<xsl:value-of select="lastname"/>
</td>
</xsl:for-each>
</tr>
<tr>
<th>Nick Name</th>
<xsl:for-each select="student">
<td>
<xsl:value-of select="nickname"/>
</td>
</xsl:for-each>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
或者,您可以创建一个列出行标签的变量并对其进行循环以消除代码重复:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="my">
<my:row-labels>
<label>First Name</label>
<label>Last Name</label>
<label>Nick Name</label>
</my:row-labels>
<xsl:template match="/class">
<xsl:variable name="student" select="student" />
<table border="1">
<xsl:for-each select="document('')/xsl:stylesheet/my:row-labels/label">
<xsl:variable name="i" select="position()"/>
<tr>
<th>
<xsl:value-of select="."/>
</th>
<xsl:for-each select="$student">
<td>
<xsl:value-of select="*[$i]"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
我希望动态转置 table 以填充 column-wise,而不是 row-wise。我的 table headers 必须先 column-wise,我的后续数据必须按该方向填充它。
为了简单起见,我创建了一个示例。
我目前的table如下:
Simple Table
不过,我希望它看起来像:
First Name| Graham | Albert | Thomas
Last Name | Bell |Einstein| Edison
Nickname| Garry | Ally | Eddy
我的XML是:
<?xml-stylesheet type = "text/xsl" href = "student.xsl"?>
<class>
<student>
<firstname>Graham</firstname>
<lastname>Bell</lastname>
<nickname>Garry</nickname>
</student>
<student>
<firstname>Albert</firstname>
<lastname>Einstein</lastname>
<nickname>Ally</nickname>
</student>
<student>
<firstname>Thomas</firstname>
<lastname>Edison</lastname>
<nickname>Eddy</nickname>
</student>
</class>
我的 XSL 是:
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/class">
<html>
<body>
<h2>Student List</h2>
<table border = "1">
<tr bgcolor="lightgreen">
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
</tr>
<xsl:for-each select = "student">
<tr>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
提前致谢!
对于通用转置,您可以执行以下操作:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/class">
<table border="1">
<xsl:for-each select="student[1]/*">
<xsl:variable name="i" select="position()"/>
<tr>
<th>
<xsl:value-of select="name()"/>
</th>
<xsl:for-each select="../../student">
<td>
<xsl:value-of select="*[$i]"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
获得:
如果您想拥有自己的标签,则需要对 table 行进行硬编码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/class">
<table border="1">
<tr>
<th>First Name</th>
<xsl:for-each select="student">
<td>
<xsl:value-of select="firstname"/>
</td>
</xsl:for-each>
</tr>
<tr>
<th>Last Name</th>
<xsl:for-each select="student">
<td>
<xsl:value-of select="lastname"/>
</td>
</xsl:for-each>
</tr>
<tr>
<th>Nick Name</th>
<xsl:for-each select="student">
<td>
<xsl:value-of select="nickname"/>
</td>
</xsl:for-each>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
或者,您可以创建一个列出行标签的变量并对其进行循环以消除代码重复:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="my">
<my:row-labels>
<label>First Name</label>
<label>Last Name</label>
<label>Nick Name</label>
</my:row-labels>
<xsl:template match="/class">
<xsl:variable name="student" select="student" />
<table border="1">
<xsl:for-each select="document('')/xsl:stylesheet/my:row-labels/label">
<xsl:variable name="i" select="position()"/>
<tr>
<th>
<xsl:value-of select="."/>
</th>
<xsl:for-each select="$student">
<td>
<xsl:value-of select="*[$i]"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>