2013-02-18 67 views
0

指定数目这里是我的XSLT:限制XSL的执行:模板的时间

<!-- 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> 

我希望能够将项目的输出具体的数字,但不是所有的人。 我该如何让“renderItem”执行不超过4次?

回答

0

针对您的问题的解决方案依赖于使用递归,手动控制模板执行的次数(或者计算项目渲染的次数)。

这是更多的一个想法,如何实现它比你的问题的解决方案(我不知道你的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() &lt;=$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> 
+0

Pablo,感谢您发布此解决方案。我会试一试。 – 2013-02-19 02:52:01

2

你的代码看起来非常奇怪:这一切的xsl:选择,的xsl:for-每个,和xsl:调用模板看起来像一个自制的实施应用,模板和模板规则。而且,xsl:call-template没有选择属性 - 这是一个语法错误,你的XSLT处理器应该这样标记它,而不是简单地忽略它。

忽略这一点,我认为你的问题最简单的解决方案是测试你是否想通过检查它在树中的位置来处理一个项目。类似于

<xsl:template match="statement"> 
    <xsl:variable name="pos"> 
    <xsl:number level="any" from="..."/> 
    </xsl:variable> 
    <xsl:if test="$pos &lt; 5">... 
</xsl:template> 
+0

迈克尔,这是一个伪代码,不是真实的东西,所以,你是对的,它最不可能工作。感谢您发布您的解决方案,我会试一试。 – 2013-02-19 02:50:53