Source XML:
<Root>
<Data>
<Value>10</Value>
</Data>
<Data>
<Value>10</Value>
</Data>
<Data>
<Value>15</Value>
</Data>
<Data>
<Value>2</Value>
</Data>
<Data>
<Value>32</Value>
</Data>
<Data>
<Value>5</Value>
</Data>
<Data>
<Value>40</Value>
</Data>
<Data>
<Value>18</Value>
</Data>
<Data>
<Value>50</Value>
</Data>
<Data>
<Value>1</Value>
</Data>
<Data>
<Value>43</Value>
</Data>
<Data>
<Value>5</Value>
</Data>
<Data>
<Value>21</Value>
</Data>
<Data>
<Value>87</Value>
</Data>
<Data>
<Value>8</Value>
</Data>
....
</Root>
XSL-FO Code: My code contains one table-row, and one table-footer
<fo:table>
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-footer>
<fo:table-row>
<fo:table-cell>
<fo:block>Subtotal: </fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<fo:retrieve-table-marker retrieve-class-name="Subtotal" retrieve-position-within-table="last-starting"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-footer>
<fo:table-body>
<xsl:for-each select="/Root/Data">
<fo:table-row>
<fo:table-cell>
<fo:block>Value:</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="Value"/>
<fo:marker marker-class-name="Subtotal">
<xsl:variable name="position" select="position()"/>
<xsl:value-of xpath="sum(../Data[position() <= $position]/Value)"/>
</fo:marker>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
I am generating PDF outputs from the above table. In PDF, the table can have up to 15 table rows per page. On each page, the subtotal needs to be calculated, by suming up only the Values displayed on the current page.
My table above (the only solution I found was using a table-marker) calculates the correct subtotal for Page1, but fails for Page2, etc. It appends the subtotals from all previous pages to the current page.
For example: if the subtotal of Value elements displayed on Page 1 is 10, and the subtotal of Values displayed on Page 2 is 20, my table will display on Page1, the subtotal as 10, but on Page2, the subtotal will 30, instead of 20. Can anyone help in solving this?
Thanks in Advance
Use "positional grouping" in XSLT 1 as in
<xsl:for-each select="Root/Data[position() mod 15 = 1]">
<xsl:variable name="data-of-page" select=". | following-sibling::Data[position() < 15]"/>
<xsl:for-each select="$data-of-page">
<fo:table-row>
<fo:table-cell>
<fo:block>Value:</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="Value"/>
<xsl:if test="position() = last()">
<fo:marker marker-class-name="Subtotal">
<xsl:value-of xpath="sum($data-of-page/Value)"/>
</fo:marker>
</xsl:if>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</xsl:for-each>