我有一个xml:
<plot>
<image>1.png</image>
</plot>
<plot>
<image>2.png</image>
</plot>
<plot>
<image>3.png</image>
</plot>
<plot>
<image>4.png</image>
</plot>
<plot>
<image>5.png</image>
</plot>
我需要显示一个包含3列的表,如果最后一列(第6列)不存在,我需要输入'---', 像这样:
| col_a | col_b | col_c | --------+-------+-------- | 1.png | 2.png | 3.png | | 4.png | 5.png | --- |
如何使用xsl模板创建此表?
答案 0 :(得分:1)
遍历每三个绘图元素,并让每次迭代输出该行的所有单元格,如下所示:
<xsl:for-each select="plot[position() mod 3 = 1]">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:choose>
<xsl:when test="following-sibling::plot[1]">
<xsl:value-of select="following-sibling::plot[1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>---</xsl:text>
</xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:choose>
<xsl:when test="following-sibling::plot[2]">
<xsl:value-of select="following-sibling::plot[2]"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>---</xsl:text>
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
如果你想要一个可以扩展到任意数量的列的解决方案,那么执行相同类型的循环,但不是显式地执行每个列,而是调用输出单元格的模板并递归调用自身,并输出剩余的单元格数量
答案 1 :(得分:0)
我可能会利用xsl:number count属性来查找数字,看看它是否可以被3整除,找出你需要的数量
然后使用foreach循环遍历元素并添加正确数量的-----。