2014-12-02 59 views
1

所以我給了一些東西來解決,並且我對XSLT的瞭解有限。我基本上想從我運行的模板中保留一個變量。模板中的XSLT返回變量

<xsl:template name="repeatable"> 
    <xsl:param name="index" select="1" /> 
    <xsl:param name="total" select="10" /> 

    <xsl:if test="not($index = $total)"> 
     <xsl:call-template name="repeatable"> 
      <xsl:with-param name="index" select="$index + 1" /> 
     </xsl:call-template> 
    </xsl:if> 
    </xsl:template> 

以上是我想從中返回變量「$ total」的模板。下面的模板是我稱之爲上述模板的模板。

<xsl:template match="randomtemplate"> 
    <xsl:call-template name="repeatable" \> 
</xsl:template> 

所以基本上,我只想讓「總」變量返回給我或以某種方式從randomtemplate訪問。

乾杯

+0

恐怕這沒有任何意義。 $ total參數的值是10.它被硬編碼到樣式表中,並且它不會改變,除非* you *改變它。 – 2014-12-02 05:23:39

回答

1

這實際上可能不是你真正需要的,但你可以做的是改變repeatable模板簡單地輸出值達到總時,就像這樣:

<xsl:template name="repeatable"> 
    <xsl:param name="index" select="1" /> 
    <xsl:param name="total" select="10" /> 

    <xsl:choose> 
     <xsl:when test="not($index = $total)"> 
     <xsl:call-template name="repeatable"> 
      <xsl:with-param name="index" select="$index + 1" /> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$index" /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

然後,您可以在xsl:variable包裹xsl:call-template捕捉到該值,然後將它輸出

<xsl:variable name="result"> 
    <xsl:call-template name="repeatable" /> 
</xsl:variable> 
<xsl:value-of select="$result" /> 

竟被這在這種情況下輸出10

+0

「*在這種情況下,會輸出」10「。*」是否有輸出其他內容的情況? – 2014-12-02 14:11:11