2013-03-04 66 views
1

我試圖計算for循環中的記錄數及其值(對於特定列)的總和。請找到下面的代碼並給我建議。XSLT 2.0如何爲每個組循環添加a的值

<xsl:for-each-group select="current-group()" group-by="CostCentre/PCCode"> 
       <!-- variables for each pc code --> 
       <xsl:variable name="chargeRate" select="SpaceTariffGroup/ChargeRate"/> 
       <xsl:variable name="dedicatedArea" 
        select="sum((current-group()[SpaceType/ChargeBackType = $chargeBackTypeDedicatedArea]/(Area*CalculationFactor)))"/>    

       <xsl:variable name="floorFactor" 
        select="$dedicatedArea div ($totalDedicatedAreaPerFloor)"/>`enter code here` 
       <xsl:variable name="floorCommonArea" 
        select="$floorFactor * $totalFloorCommonAreaPerFloor"/> 
       <xsl:variable name="dedicatedAndFloorCommonArea" 
        select="($dedicatedArea + $floorCommonArea)"/>      

       <xsl:variable name="totalChargedArea" 
        select="$dedicatedAndFloorCommonArea"/> 
       <xsl:variable name="totalChargedAmount" 
        select="$chargeRate * $totalChargedArea"/> 
<xsl:value-of select="$totalChargedArea"/> 
<xsl:text>,</xsl:text> 
<xsl:value-of select="$totalChargedAmount"/> 
enter code here 
</xsl:for-each-group> 

會打印這樣的東西。 100,5000 200,5000 300,5000 400,5000

我想這樣的輸出中。 100,5000 200,5000 300,5000 400,5000 4,20000(這最後一排是總的記錄,這些記錄的值之和)不知道我怎樣才能得到它

回答

2

那麼你可以將for-each-group的結果存儲在一個變量中,例如

<xsl:variable name="charges"> 
<xsl:for-each-group select="current-group()" group-by="CostCentre/PCCode"> 
       <!-- variables for each pc code --> 
       <xsl:variable name="chargeRate" select="SpaceTariffGroup/ChargeRate"/> 
       <xsl:variable name="dedicatedArea" 
        select="sum((current-group()[SpaceType/ChargeBackType = $chargeBackTypeDedicatedArea]/(Area*CalculationFactor)))"/>    

       <xsl:variable name="floorFactor" 
        select="$dedicatedArea div ($totalDedicatedAreaPerFloor)"/>`enter code here` 
       <xsl:variable name="floorCommonArea" 
        select="$floorFactor * $totalFloorCommonAreaPerFloor"/> 
       <xsl:variable name="dedicatedAndFloorCommonArea" 
        select="($dedicatedArea + $floorCommonArea)"/>      

       <xsl:variable name="totalChargedArea" 
        select="$dedicatedAndFloorCommonArea"/> 
       <xsl:variable name="totalChargedAmount" 
        select="$chargeRate * $totalChargedArea"/> 
<charge> 
    <area><xsl:value-of select="$totalChargedArea"/></area> 
    <amount><xsl:value-of select="$totalChargedAmount"/></amount> 
    </charge> 
</xsl:for-each-group> 
</xsl:variable> 

然後你可以用例如,

<xsl:for-each select="$charges/charge"> 
    <xsl:value-of select="concat(area, ',', amount)"/> 
</xsl:for-each> 

和輸出與

<xsl:value-of select="sum($charges/charge/amount)"/> 
總和