我有以下XML结构:
<Tab4>
<T4Head1>First Table header 1</T4Head1>
<T4Head2>First Table header 2</T4Head2>
<T4Head3>First Table header 3</T4Head3>
<T4Head4>First Table header 4</T4Head4>
</Tab4>
<Tab4>
<T4Col1>First Table row 1 column 1</T4Col1>
<T4Col2>First Table row 1 column 2</T4Col2>
<T4Col3>First Table row 1 column 3</T4Col3>
<T4Col4>First Table row 1 column 4</T4Col4>
</Tab4>
another element than Tab4 might occur here...
<Tab4>
<T4Head1>Second Table header 1</T4Head1>
<T4Head2>Second Table header 2</T4Head2>
<T4Head3>Second Table header 3</T4Head3>
<T4Head4>Second Table header 4</T4Head4>
</Tab4>
<Tab4>
<T4Col1>Second Table row 1 column 1</T4Col1>
<T4Col2>Second Table row 1 column 2</T4Col2>
<T4Col3>Second Table row 1 column 3</T4Col3>
<T4Col4>Second Table row 1 column 4</T4Col4>
</Tab4>
<Tab4>
<T4Col1>Second Table row 2 column 1</T4Col1>
<T4Col2>Second Table row 2 column 2</T4Col2>
<T4Col3>Second Table row 2 column 3</T4Col3>
<T4Col4>Second Table row 2 column 4</T4Col4>
</Tab4>
<Tab4>
<T4Col1>Second Table row 3 column 1</T4Col1>
<T4Col2>Second Table row 3 column 2</T4Col2>
<T4Col3>Second Table row 3 column 3</T4Col3>
<T4Col4>Second Table row 3 column 4</T4Col4>
</Tab4>
another element than Tab4 might occur here...
使用XSLT 1.0我需要输出如下所示的html。换句话说,我需要每个包含T4Col1,T4Col2等的Tab4块重复,直到出现下一种元素(不是Tab4 / T4Col1)。
<tr>
<td>
<table class="specTab" border="1">
<tbody>
<tr>
<th>
Table header 1
</th>
<th>
Table header 2
</th>
<th>
Table header 3
</th>
<th>
Table header 4
</th>
</tr>
<!-- this part should repeat -->
<tr>
<td>
Table column 1
</td>
<td>
Table column 2
</td>
<td>
Table column 3
</td>
<td>
Table column 4
</td>
</tr>
<!-- this part should repeat -->
</tbody>
</table>
</td>
</tr>
我有一些尝试,但无法控制它。我当前的XSLT代码如下所示:
<xsl:for-each select="./node()">
<xsl:choose>
<!-- Multiple other test scenarios -->
<xsl:when test="name()='Rub1' and . !=''">
</xsl:when>
<xsl:when test="name()='Tab4' and ./T4Head1">
<tr>
<td>
<table class="specTab" border="1">
<tbody>
<tr>
<th>
<xsl:value-of select="./T4Head1"/>
</th>
<th>
<xsl:value-of select="./T4Head2"/>
</th>
<th>
<xsl:value-of select="./T4Head3"/>
</th>
<th>
<xsl:value-of select="./T4Head4"/>
</th>
</tr>
<xsl:for-each select="following-sibling::node()/T4Col1">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
上面代码的问题是它输出所有Tab4 / T4Col1(1 + 3),甚至是下一个标题出现后的那些。我需要对标题和所有行/列进行分组,因为我希望它们位于同一个HTML表中。
感谢我能得到的任何帮助。
答案 0 :(得分:0)
考虑以下示例:
XML
<root>
<p>para 1</p>
<Tab4>
<T4Head1>First Table header 1</T4Head1>
<T4Head2>First Table header 2</T4Head2>
<T4Head3>First Table header 3</T4Head3>
<T4Head4>First Table header 4</T4Head4>
</Tab4>
<Tab4>
<T4Col1>First Table row 1 column 1</T4Col1>
<T4Col2>First Table row 1 column 2</T4Col2>
<T4Col3>First Table row 1 column 3</T4Col3>
<T4Col4>First Table row 1 column 4</T4Col4>
</Tab4>
<p>para 2</p>
<Tab4>
<T4Head1>Second Table header 1</T4Head1>
<T4Head2>Second Table header 2</T4Head2>
<T4Head3>Second Table header 3</T4Head3>
<T4Head4>Second Table header 4</T4Head4>
</Tab4>
<Tab4>
<T4Col1>Second Table row 1 column 1</T4Col1>
<T4Col2>Second Table row 1 column 2</T4Col2>
<T4Col3>Second Table row 1 column 3</T4Col3>
<T4Col4>Second Table row 1 column 4</T4Col4>
</Tab4>
<Tab4>
<T4Col1>Second Table row 2 column 1</T4Col1>
<T4Col2>Second Table row 2 column 2</T4Col2>
<T4Col3>Second Table row 2 column 3</T4Col3>
<T4Col4>Second Table row 2 column 4</T4Col4>
</Tab4>
<Tab4>
<T4Col1>Second Table row 3 column 1</T4Col1>
<T4Col2>Second Table row 3 column 2</T4Col2>
<T4Col3>Second Table row 3 column 3</T4Col3>
<T4Col4>Second Table row 3 column 4</T4Col4>
</Tab4>
<p>para 3</p>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="rows" match="Tab4[T4Col1]" use="generate-id(preceding-sibling::Tab4[T4Head1][1])" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Tab4[T4Head1]">
<table border="1">
<thead>
<tr>
<xsl:apply-templates/>
</tr>
</thead>
<tbody>
<xsl:for-each select="key('rows', generate-id())">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
<xsl:template match="Tab4[T4Col1]"/>
<xsl:template match="Tab4[T4Head1]/*">
<th>
<xsl:apply-templates/>
</th>
</xsl:template>
<xsl:template match="Tab4[T4Col1]/*">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
</xsl:stylesheet>
这会使用 key 将正文行绑定到标题行,以便可以通过标题行的唯一ID来调用它们。
结果将是:
<root>
<p>para 1</p>
<table border="1">
<thead>
<tr>
<th>First Table header 1</th>
<th>First Table header 2</th>
<th>First Table header 3</th>
<th>First Table header 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>First Table row 1 column 1</td>
<td>First Table row 1 column 2</td>
<td>First Table row 1 column 3</td>
<td>First Table row 1 column 4</td>
</tr>
</tbody>
</table>
<p>para 2</p>
<table border="1">
<thead>
<tr>
<th>Second Table header 1</th>
<th>Second Table header 2</th>
<th>Second Table header 3</th>
<th>Second Table header 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Second Table row 1 column 1</td>
<td>Second Table row 1 column 2</td>
<td>Second Table row 1 column 3</td>
<td>Second Table row 1 column 4</td>
</tr>
<tr>
<td>Second Table row 2 column 1</td>
<td>Second Table row 2 column 2</td>
<td>Second Table row 2 column 3</td>
<td>Second Table row 2 column 4</td>
</tr>
<tr>
<td>Second Table row 3 column 1</td>
<td>Second Table row 3 column 2</td>
<td>Second Table row 3 column 3</td>
<td>Second Table row 3 column 4</td>
</tr>
</tbody>
</table>
<p>para 3</p>
</root>
呈现为: