1
我想創建1個XSL樣式表,可以創建具有數據特定標題和列數的HTML表格代碼。下面是數據的兩個示例性套欲由1個樣式表用於動態XML數據的XSL樣式表
<root>
<row>
<postId>1</postId>
<id>1</id>
<name>name 1</name>
<email>[email protected]</email>
<body>body 1</body>
</row>
<row>
<postId>1</postId>
<id>2</id>
<name>name 2</name>
<email>[email protected]</email>
<body>body 2</body>
</row>
</root>
<root>
<row>
<id>1</id>
<desc>desc 1</desc>
<Note>Note 1</Note>
</row>
<row>
<id>2</id>
<desc>desc 2</desc>
<Note>Note 2</Note>
</row>
<row>
<id>3</id>
<desc>desc 3</desc>
<Note>Note 3</Note>
</row>
</root>
處理然後,我想在片材產生類似輸出:
<table>
<thead>
<th>postId</th><th>id</th><th>name</th><th>email</th><th>body</th>
</thead>
<tbody>
<tr>
<td>1</td><td>1</td><td>mane 1</td><td>[email protected]</td><td>body 1</td>
</tr>
<tr>
<td>1</td><td>2</td><td>mane 2</td><td>[email protected]</td><td>body 2</td>
</tr>
</tbody>
<table>
<thead>
<th>id</th><th>desc</th><th>Note</th>
</thead>
<tbody>
<tr>
<td>1</td><td>desc 1</td><td>Note 1</td>
</tr>
<tr>
<td>2</td><td>desc 2</td><td>Note 2</td>
</tr>
<tr>
<td>3</td><td>desc 3</td><td>Note 3</td>
</tr>
</tbody>
</table>
我已想出表頭(見下面),但我似乎無法讓身體工作。我無法弄清楚如何顯示一個元素的值,我不知道這個元素的名稱或者是否存在。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<html>
<body>
<h2>My Table Data</h2>
<table border="1">
<thead>
<tr bgcolor="#9acd32">
<xsl:for-each select="row[1]/*">
<th><xsl:value-of select ="name(.)"/></th>
</xsl:for-each>
</tr>
</thead>
<tbody>
?????
</tbody>
非常感謝! –