您的问题的解决方案依赖于使用递归,手动控制模板执行的次数(或计算项目呈现的次数)。
与其说是解决您的问题,不如说是如何实现它的想法(我不知道您的 XML 文件是什么样子),但是我尝试调整您发布的代码(可能不正确,没有我的 XML没有把握)。
<xsl:template match="renderItems">
<!-- Set of items to process -->
<xsl:param name="items" />
<!-- Tracks number of times that this template is going to be executed -->
<xsl:param name="count" />
<!-- Check if we have available executions -->
<xsl:if test="$count > 0">
<!-- Define whether the current node is a single item or a category of items -->
<xsl:choose>
<!-- Category of items -->
<xsl:when test="$items/statement">
<!-- Select the number of items which are going to be rendered taking into
account the count parameter -->
<xsl:variable name="items-to-render"
select="$items/statement[position() <=$count]" />
<!-- Render item in this category -->
<xsl:for-each select="$items-to-render">
<!--
Do whatever you have to do with each item
-->
</xsl:for-each>
<!-- Call this template again with the updated values -->
<xsl:call-template name="renderItems">
<!-- Remove the category of items from the items to be processed -->
<xsl:with-param name="items"
select="$items[position() > 1]" />
<!-- Extract from the count, the number of items that we already processed -->
<xsl:with-param name="count"
select="$count - count($items-to-render)" />
</xsl:call-template>
</xsl:when>
<!-- Single item -->
<xsl:otherwise>
<!-- Render this item -->
<!--
Do whatever you have to do with each item
-->
<!-- Call this template again with the updated values -->
<xsl:call-template name="renderItems">
<!-- Remove this item from the items to be processed -->
<xsl:with-param name="items"
select="$items[position() > 1]" />
<!-- One item less to process -->
<xsl:with-param name="count"
select="$count - 1" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
然后你可以使用(或类似的东西)调用模板。
<xsl:call-template name="renderItems">
<xsl:with-param name="items"
select="statement" />
</xsl:call-template>