将节点序列渲染为 M x N 表 [英] Rendering a node sequence as M x N table
问题描述
@Oded:对不起,我的阐述很差...我的输入文档有一个这样的片段:
@Oded: Sorry to have been poor in my exposition... My input document has a fragment like this:
<recordset name="resId" >
<record n="0">example 1</record>
<record n="1">example 2</record>
<record n="2">example 1</record>
....
<record n="N">example 1</record>
</recordset>
包含任意长的节点序列.属性n"报告节点在序列中的顺序.我需要将该序列排列为 M(行)x N(列)表中的输出,但我在这样做时遇到了一些麻烦.我不能调用模板
containing an arbitrarily long node sequence. The attribute "n" reports the order of the node in the sequence. I need to arrange as output that sequence in a M (rows) x N (columns) table and I have some trouble doing that. I cannot call a template
<xsl:template match="recordset">
<table>
<xsl:apply-templates select="record"/>
</table>
</xsl:template>
类似于:
<xsl:template match="record">
<xsl:if test="@n mod 3 = 0">
<tr>
</xsl:if>
........
<td><xsl:value-of select"something"></td>
因为代码无效(我应该在模板末尾以某种方式重复它)我必须对 numbered 属性的存在给予一些(也许太多)信任.有人有提示吗?谢谢!
because code is invalid (and I should repeat it at the end of the template in some way) and I must put some (maybe too much) trust in the presence of the numbered attribute. Someone has a hint? Thanks!
推荐答案
您必须确保嵌套永远不会被破坏.您希望在输出中嵌套的内容必须在 XSLT 中嵌套.
You must ensure that nesting is never broken. Things you want nested in the output must be nested in the XSLT.
<xsl:variable name="perRow" select="3" />
<xsl:template match="recordset">
<table>
<xsl:apply-templates
mode = "tr"
select = "record[position() mod $perRow = 1]"
/>
</table>
</xsl:template>
<xsl:template match="record" mode="tr">
<tr>
<xsl:variable name="td" select="
. | following-sibling::record[position() < $perRow]
" />
<xsl:apply-templates mode="td" select="$td" />
<!-- fill up the last row -->
<xsl:if test="count($td) < $perRow">
<xsl:call-template name="filler">
<xsl:with-param name="rest" select="$perRow - count($td)" />
</xsl:call-template>
</xsl:if>
</tr>
</xsl:template>
<xsl:template match="record" mode="td">
<td>
<xsl:value-of select="." />
</td>
</xsl:template>
<xsl:template name="filler">
<xsl:param name="rest" select="0" />
<xsl:if test="$rest">
<td />
<xsl:call-template name="filler">
<xsl:with-param name="rest" select="$rest - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
这篇关于将节点序列渲染为 M x N 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!