2010-02-06 77 views
0

什麼是錯在此代碼

<xsl:apply-templates select="xslTutorial"/> 
</xsl:template> 
<xsl:template match="xslTutorial"> 
<p> 
     <xsl:for-each select="number"> 

     <xsl:value-of select="."/>+ 
     </xsl:for-each> 
     = 
     <xsl:value-of select="sum(number)"/> 
</p> 
</xsl:template> 

在結果中有一個「+」,我不它顯示我想要的結果是 1 + 3 + 17 + 11 = 32 但結果是1 + 3 + 17 + 11 + 32 = 我做什麼,以防止最後+

回答

3

你需要確保最後一次迭代中不包括 「+」:

<xsl:apply-templates select="xslTutorial"/> 

<!--</xsl:template> was this a typo? --> 

<xsl:template match="xslTutorial"> 
    <p> 
    <xsl:for-each select="number"> 
     <xsl:value-of select="."/> 
     <xsl:if test="position() != last()">+</xsl:if> 
    </xsl:for-each> 
    = 
    <xsl:value-of select="sum(number)"/> 
    </p> 
</xsl:template> 
0

您沒有任何邏輯可以阻止它輸出「+」。

像這樣的東西會工作: