0

Here's my XSLT:

<!-- Looping through both Items and Categories of Items -->
<xsl:for-each select="statement">

    <!-- Define whether current node is a single item or a category of items -->
    <xsl:choose>

        <!-- Category of items -->
        <xsl:when test="statement">

            <!-- Render all items in this category -->
            <xsl:for-each select="statement">
                <xsl:call-template name="renderItem" select="current()" />
            </xsl:for-each>

        </xsl:when>

        <!-- Single item -->
        <xsl:otherwise>
            <xsl:call-template name="renderItem" select="." />
        </xsl:otherwise>

    </xsl:choose>

</xsl:for-each>

I want to be able to output specific number of items, but not all of them. How do I make "renderItem" to be executed not more than, say, 4 times?

4

2 に答える 2

2

あなたのコードはかなり奇妙に見えます: この xsl:choose、xsl:for-each、および xsl:call-template はすべて、apply-template とテンプレート ルールの自家製の実装のように見えます。さらに、xsl:call-template は select 属性を取りません。これは構文エラーであり、XSLT プロセッサは単純に無視するのではなく、そのようにフラグを立てる必要があります。

それを無視して、問題の最も簡単な解決策は、ツリー内の位置を調べてアイテムを処理するかどうかをテストすることだと思います。何かのようなもの

<xsl:template match="statement">
  <xsl:variable name="pos">
    <xsl:number level="any" from="..."/>
  </xsl:variable>
  <xsl:if test="$pos &lt; 5">...
</xsl:template>
于 2013-02-18T12:53:12.960 に答える