2013-02-08 40 views
0

我想要得到的總和。這是xslt代碼。如何獲得屬性中的值的總和在xslt

<xsl:template match="Entry"> 
    <xsl:if test="position() &lt;= 10"> 


    <tr> 
     <td> 
     <xsl:value-of select="substring-before(@Value,'||')"/> 
     </td> 

     <td> 
     <xsl:value-of select="format-number(substring(substring-after(@Value,'||||'),1,10),'#.#')"/> 

     </td> 
    </tr> 

</xsl:if> 


    </xsl:template> 

上面的代碼會將數據填充爲兩個顏色。沒關係。現在我需要得到的總和<xsl:value-of select="format-number(substring(substring-after(@Value,'||||'),1,10),'#.#')"/> 我來自程序編程。我讀了很多文章,但我仍然想知道如何得到這個總和。有誰能夠幫助我?

這裏是XML

<TopHoldings Currency="xxx"> 
      <Entry Type="CName||C||S||Fund Weight (%)||Benchmark weight (%)" Value="Ab||U||||1.2170000000000||" Date="8/31/2011" /> 

這裏是整個XSLT

 <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0"> 
      <tr style="width:50%; text-align:left;background-color:E6F1F9;"> 
      <th>  </th> 
      <th>  % of funds  </th> 
     </tr> 
     <xsl:apply-templates select="$items"> 
       <xsl:sort select="format-number(substring(substring-after(@Value,'||||'),1,10),'#.#')" order="descending"/> 
       <xsl:sort select="substring-before(@Value,'||')"/> 
     </xsl:apply-templates> 


     </table> 

     </body> 

    </html> 

    </xsl:template> 

    <xsl:template match="Entry"> 
    <xsl:if test="position() &lt;= 10"> 


    <tr> 
     <td> 
     <xsl:value-of select="substring-before(@Value,'||')"/> 
     </td> 

     <td> 
     <xsl:value-of select="format-number(substring(substring-after(@Value,'||||'),1,10),'#.#')"/> 

     </td> 
    </tr> 

</xsl:if> 


    </xsl:template> 



</xsl:stylesheet> 
+0

您能否向我們展示您引用此模板的模板?您是否只想總結前10個值? – JLRishe

+0

是的我想獲得十個值的總和 – newday

+0

並且關於我對更大的XSLT樣本...?您使用什麼處理器來執行您的XSLT? – JLRishe

回答

2

當你需要總結的XSLT 1.0你有多個值的值依賴於遞歸[編輯:在XSLT 1.0中函數總和也可用](在XSLT 2.0中有一個XPath函數sum( ))。

以下模板通過elements-to-sum參數執行給定元素的總和,提取值以按照您指定的屬性@Value求和。

<?xml version="1.0" encoding="utf-8" ?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       version="1.0"> 

<xsl:output method="html" /> 

<xsl:template match="TopHoldings"> 
    <table> 
     <xsl:call-template name="sum"> 
      <xsl:with-param name="elements-to-sum" 
          select="Entry" /> 
     </xsl:call-template> 
    </table> 
</xsl:template> 

<xsl:template name="sum"> 
    <xsl:param name="elements-to-sum" /> 

    <xsl:param name="sum" select="'0'" /> 

    <!-- Store in variables the following operations to avoid performing them more than once --> 
    <xsl:variable name="current-element" select="$elements-to-sum[1]" /> 
    <xsl:variable name="current-value" select="format-number(substring(substring-after($current-element/@Value,'||||'),1,10),'#.#')" /> 

    <!-- Output the table row --> 
    <tr> 
     <td><xsl:value-of select="substring-before($current-element/@Value, '||')" /></td> 
     <td><xsl:value-of select="$current-value" /></td> 
    </tr> 

    <!-- Determine whether continue --> 
    <xsl:choose> 
     <!-- Case END: we have just one element to sum so we perform the sum and we output the desired result --> 
     <xsl:when test="count($elements-to-sum) = 1"> 
      <tr> 
       <td>Result:</td> 
       <td><xsl:value-of select="$current-value + $sum" /></td> 
      </tr> 
     </xsl:when> 
     <!-- Case RECURSION : we call this template again adding the current value to the sum and removing the first element from the parameter elements-to-sum --> 
     <xsl:otherwise> 
      <xsl:call-template name="sum"> 
       <xsl:with-param name="elements-to-sum" 
           select="$elements-to-sum[position() > 1]" /> 
       <xsl:with-param name="sum" 
           select="$sum + $current-value" /> 
      </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 

</xsl:template> 

</xsl:stylesheet> 

我假設你想顯示總和的結果作爲在同一個表中的新行,如果情況並非如此,你要顯示的結果在其他地方的解決方案將略有不同。告訴我,如果這個解決方案對你來說是可以接受的,或者你需要在元素外面顯示總和。

注意:不要在屬性@Value中執行這些'複雜'的字符串操作,我會考慮將該屬性中的所有信息分成不同的屬性。

+0

順便說一句,我發佈此代碼之前知道你張貼你的。如果您無法完成工作,請告訴我,我會將其重寫爲您當前的代碼。 –

+0

如果您可以將新代碼添加爲解決方案。 – newday

+0

只有兩個問題: 1.你將如何處理值的總和?因爲在您發佈的XSLT中,我無法看到打印結果的位置。 2.您是否使用支持節點集擴展功能的XSLT處理器? –

0

只需使用XPath函數sum(),就可以在XSLT 2.0的XPath 2.0和XSLT 1.0的XPath 1.0中使用它,如http://www.w3.org/TR/xpath/#function-sum中所述。如果你想得到總和的數字是屬性「Value」到一個元素「Entry」,使用sum(// Entry/@ Value)來抓取所有數據並進行總結。 xml數據

+0

你確定嗎? sum不會將所有屬性相加在一起。這就是遞歸的原因。如果它是元素節點,那麼我們可以使用sum。如果我錯了,請糾正我。 – newday

+0

@pottuamman:是的,我嘗試和sum函數處理屬性 - 但前提是函數可以從節點列表中抓取它們(甚至屬性在技術上是節點)。同時我意識到你嘗試求和的數據只是屬性字符串的一部分,所以你用「substring」做一些解開。因爲然後沒有總和可以工作的節點列表,它會失敗。嘗試將信息分成自己的屬性。你可以做一個準備XSL轉換來獲得適當的層次結構,並就此進行總結。 – Andreas