[xsl] subtotals of existing sums based on grouping

Subject: [xsl] subtotals of existing sums based on grouping
From: dmitrik@xxxxxxxxxxxxxx
Date: Thu, 6 Jan 2005 19:09:30 -0500 (GMT-05:00)
The xsl code below  is showing sums for mutiple records, (some repeating code for multiple columns has been 
removed for this question): 


abc  23 50
abc  10  20
def   3  3


<?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" indent="yes" />

 <xsl:variable name="global">
   <xsl:value-of select="Portfilio/AsOf" />
 </xsl:variable>


<xsl:template match="/">
   <html>
     <head>
       <title>Portfilio Bucketed</title>
     </head>
     <body>           
	<table border="0" width="100%" cellpadding="10">
	<tr><b>Report</b></tr>
	<tr><b>IDF</b></tr>
	<tr><b>As Of: <xsl:value-of select="$global" /></b></tr>
	</table>
       <xsl:apply-templates />
     </body>
   </html>
</xsl:template>

<xsl:template match="Portfilio">


<html>
   <style>
   td { width: 120px; font-family:verdana; font-size:10px; }
    .r0 {background-color: #f0f0f0}
    .r1 {background-color: white}
   </style>    
 
     <body>       
     <table border=".5">
     <tr>    
      <td ><b>Customer</b></td>
      <td ><b>Trade Id</b></td>
      <td ><b>Type</b></td>
      <td align="center"><b>0</b></td>
      <td align="center"><b>3</b></td>
      <td align="center"><b>6</b></td>
      <td align="center"><b>9</b></td>
      <td align="center"><b>12</b></td>
      <td align="center"><b>15</b></td>
      <td align="center"><b>18</b></td> 
      <td align="center"><b>21</b></td>                           
      <td align="center"><b>24</b></td>                           
      <td align="center"><b>27</b></td>                           
      <td align="center"><b>30</b></td>                           
      <td align="center"><b>TradeId Totals</b></td>   
     </tr>

<xsl:for-each select="Trade">

<xsl:sort select="Customer"/> 

<tr class="r{position() mod 2}">

<td><xsl:value-of select="Customer"/> </td>

<td><xsl:value-of select="TradeId"/><xsl:text/></td>

<td><xsl:value-of select="Type"/><xsl:text/></td>

<td align="right">
<xsl:value-of select="format-number(sum(Step
   [concat(substring(MinFlowDate,7),substring(MinFlowDate,1,2),substring(MinFlowDate,4,2))&gt;=
concat(substring($global,7),substring($global,1,2),substring($global,4,2))]
   [concat(substring(MinFlowDate,7),substring(MinFlowDate,1,2),substring(MinFlowDate,4,2))&lt;
concat(substring($global,7)+3,substring($global,1,2),substring($global,4,2))]
   /StepCharge), '###,###,##0')"/>
</td>


</tr>

</xsl:for-each>

<tr>
<td><b>Yearly Totals</b> </td>
<td></td>
<td></td>

<td align="right"><b>
<xsl:text/>
<xsl:value-of select="format-number(sum(Trade/Step
   [concat(substring(MinFlowDate,7),substring(MinFlowDate,1,2),substring(MinFlowDate,4,2))&gt;=
concat(substring($global,7),substring($global,1,2),substring($global,4,2))]
   [concat(substring(MinFlowDate,7),substring(MinFlowDate,1,2),substring(MinFlowDate,4,2))&lt;
concat(substring($global,7)+3,substring($global,1,2),substring($global,4,2))]
   /StepCharge), '###,###,##0')"/>
</b></td>


</tr>
  </table>
   </body>
  </html>


</xsl:template>

</xsl:stylesheet>

the xml looks like:

  <Portfilio>
  <AsOf>12/31/2004</AsOf> 
  <Trade>
  <TradeId>181747N</TradeId>   
  <Customer>Test1</Customer> 
  <Step>
  <StepCharge>-73</StepCharge> 
  <StepSetoff>24929</StepSetoff> 
  </Step>
  <Step>
  </Trade>
 <Trade>
  <TradeId>182747N</TradeId>   
  <Customer>Test1</Customer> 
  <Step>
  <StepCharge>-73</StepCharge> 
  <StepSetoff>24929</StepSetoff> 
  </Step>
  <Step>
  </Trade>
  <Trade>
  <TradeId>183747N</TradeId>   
  <Customer>Test2</Customer> 
  <Step>
  <StepCharge>-73</StepCharge> 
  <StepSetoff>24929</StepSetoff> 
  </Step>
  <Step>
  </Trade>

  </Portfilio>



the following code  groups trades , but how is it possible to 
combine the xsl files so that sum subtotals based on the trade will appear under the last trade in the above xsl?
(for example there are two test1 trades above, and one test2, and there needs to be a subtotal 
under each

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes"/>

  <xsl:key name="keyname" match="Trade" use="Customer" />
<xsl:template match="Portfilio">
   <summary>

      <xsl:for-each select="Trade[generate-id() =generate-id(key('keyname',Customer))]">
       <xsl:sort select="Customer"/>
       <summaryGroup>
         <summaryField>

            <xsl:value-of select="Customer"/>
	   <xsl:value-of select="Customer/StepCharge"/>
         </summaryField>
         <summaryvalue>
          <xsl:value-of select="sum(key('keyname',Customer/StepCharge)/Customer/StepCharge)"/>
         </summaryvalue>
       </summaryGroup>
     </xsl:for-each>
   </summary>
</xsl:template>
</xsl:stylesheet>

Thanks,
Dmitri

Current Thread