title: XSLT: Subtotals
body: <p>Source XML:</p>
  
  <pre><code>&lt;Root&gt;
      &lt;Data&gt;
          &lt;Code&gt;A&lt;/Code&gt;
          &lt;Value&gt;10&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;A&lt;/Code&gt;
          &lt;Value&gt;10&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;B&lt;/Code&gt;
          &lt;Value&gt;10&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;A&lt;/Code&gt;
          &lt;Value&gt;2&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;C&lt;/Code&gt;
          &lt;Value&gt;10&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;A&lt;/Code&gt;
          &lt;Value&gt;5&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;B&lt;/Code&gt;
          &lt;Value&gt;4&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;A&lt;/Code&gt;
          &lt;Value&gt;10&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;C&lt;/Code&gt;
          &lt;Value&gt;10&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;B&lt;/Code&gt;
          &lt;Value&gt;10&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;A&lt;/Code&gt;
          &lt;Value&gt;10&lt;/Value&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
          &lt;Code&gt;C&lt;/Code&gt;
          &lt;Value&gt;5&lt;/Value&gt;
      &lt;/Data&gt;
  ....
  &lt;/Root&gt;
  </code></pre>
  
  <p>XSL-FO Code:</p>
  
  <p>My code(XSL-FO) contains 3 columns where each column contains the content of 'A', 'B', 'C'</p>
  
  <pre><code>&lt;fo:table-body&gt;
      &lt;xfd:table-row-repeat xpath="Root/Data" font-family="Arial Narrow" font-size="10pt" padding-after="0.55cm"&gt;
          &lt;xsl:if test="Code='A'"&gt;
          &lt;fo:table-cell&gt;
          &lt;fo:block height="12pt"&gt;Value
          &lt;/fo:block&gt;
          &lt;/fo:table-cell&gt;
          &lt;fo:table-cell&gt;
          &lt;fo:block height="12pt" border="0.1pt solid black" text-align="center"&gt;
              &lt;xsl:value-of select="Value" /&gt; 
          &lt;/fo:block&gt;
          &lt;/fo:table-cell&gt;
          &lt;fo:table-cell&gt;
          &lt;fo:block height="12pt"&gt;Points&lt;/fo:block&gt; 
          &lt;/fo:table-cell&gt;
          &lt;/xsl:if&gt;
      &lt;/xfd:table-row-repeat&gt;
  &lt;/fo:table-body&gt;
  </code></pre>
  
  <p>Same code for each columns to display values of 'B' &amp; 'C'
  In Table-footer i've to get these subtotal of 'A','B', 'C'</p>
  
  <pre><code>&lt;fo:table-body&gt;
      &lt;xfd:table-row-repeat xpath="Root/Data" font-family="Arial Narrow" font-size="10pt" padding-after="0.55cm"&gt;
          &lt;xsl:if test="Code='A'"&gt;
          &lt;fo:table-cell&gt;
          &lt;fo:block height="12pt"&gt;SubTotal
          &lt;/fo:block&gt;
          &lt;/fo:table-cell&gt;
          &lt;fo:table-cell&gt;
          &lt;fo:block height="12pt" border="0.1pt solid black" text-align="center"&gt;
              &lt;--Here Sum of first 15 A's. if the A's or B's or C's exceed by 15, then the table flows to 2nd Page. In that case, 1st Page table-footer shows individual subtotals of first 15 A's, 15 B's and C's. In 2nd Page, the subtotals should contain Subtotal of first 15 A's+ Succeeding A's, in the same way B's and C's --&gt; 
          &lt;/fo:block&gt;
          &lt;/fo:table-cell&gt;
          &lt;fo:table-cell&gt;
          &lt;fo:block height="12pt"&gt;Points&lt;/fo:block&gt; 
          &lt;/fo:table-cell&gt;
          &lt;/xsl:if&gt;
      &lt;/xfd:table-row-repeat&gt;
  &lt;/fo:table-body&gt;
  </code></pre>
  
  <p>Here the XSL-FO code is shown for only one column(For Root/Data/Code='A'), the other 2 columns('B' &amp; 'C') consists of same code.</p>
  
  <p>Conditions in Detail:</p>
  
  <pre><code>Condition 1): when Root/Data/Code = 'A' or 'B' or 'C'
     i need individual totals of 'A', 'B' and 'C' in Table-Footer individual Column.
  
  Condition 2): inturn if individual count(Root/Data/Code) of 'A', 'B' &amp; 'C' crosses 15.          Then Page flows to 2nd Page then Table-Footer in 2nd Page needs to contains subtotal of first 15 A's + the sum of succeeding A's in the same way for B's And C's
  </code></pre>
  
  <p>i.e., if 20 A's, 10 B's and 25 C's are present in Source XML.</p>
  
  <pre><code>In 1st Page, Table-Footer
  
  SubtotalI(Value of 15 A's)=
  SubtotalII(Value 10 B's)=   
  SubtotalIII(Value 15 C's)=
  
  In 2nd Page, Table-Footer
  
  SubtotalI(15 A's+ next 5 A's)=   
  SubtotalII(Value 10 B's)= &lt;!--No Change as count of B's is less than 15 --&gt;
  SubtotalIII(15 C's + next 10 C's)=
  </code></pre>
  
  <p>I'm trying this logic using xsl:key by grouping through <strong>Code tag</strong> for evaluating sum of 'A', 'B' and 'C'. As i'm new to XSLT i'm finding it too difficult to solve this logic using xsl:key. Can anyone help in solving this logic?</p>
  
  <p>Thanks in Advance</p>