2013-02-18 66 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