Elements subtotal at the end of each page

Inserting subtotals on each page in a document template can be done by using the XSL-FO marker and retrieve-table-marker elements.

Steps:

  1. After creating the dynamic table in EDS Publisher, switch to XML View (View tab -> XML View)
  2. Mark the table cell on which you want to display the subtotal. It can be done by using the "fo:retrieve-table-marker" element. Note that for this element, you need to set a class name by using the "retrieve-class-name" attribute;
  3. Add the marked value on the repeating row.

Example:

1. Let's assume you have an XML file like the one below:
<balances>
     <balance>
        <value type="float">235300.0</value>
        <year type="integer">2007</year>
        <age type="integer">32</age>
    </balance>
    <balance>
        <value type="float">246680.0</value>
        <year type="integer">2008</year>
        <age type="integer">33</age>
    </balance>
...
</balances>

Now, in the template, you display the data in a table with three columns, a header and a footer.
The structure from EDS Publisher will look like this:
<fo:table>
   <fo:table-column column-width="proportional-column-width(33.3in)"/>
   <fo:table-column column-width="proportional-column-width(33.3in)"/>
   <fo:table-column column-width="proportional-column-width(33.3in)"/>
   <fo:table-header> .. </fo:table-header>
   <fo:table-footer>
      <fo:table-cell number-columns-spanned="2"> Subtotal: </table-cell>
      <fo:table-cell> <!-- Subtotal Value --> </fo:table-cell>
   </fo:table-footer>
   <fo:table-body>
      <xfd:table-row-repeat xpath="/balances/balance">
         <fo:table-cell><xfd:field xpath="value"/> </fo:table-cell>
         <fo:table-cell><xfd:field xpath="year"/></fo:table-cell>
         <fo:table-cell><xfd:field xpath="type"/></fo:table-cell>
      </xfd:table-row-repeat>
   </fo:table-body>
</fo:table>

What we want is to to create a subtotal field for the "value" element, that should appear at the end of each page. This subtotal will display the sum of all "value" elements showed until that page. 

2. We aim to add the value of the Subtotal in the last cell of the footer row. To do this, we will use the "fo:retrieve-table-marker" element. The code from the cell should look like below:

<fo:table-cell>
   <fo:block>
      <fo:retrieve-table-marker retrieve-class-name="TABLE_FOOTER_SUBTOTAL" retrieve-position-within-table="last-starting"/>
   </fo:block>
</fo:table-cell>

3. Next, we need to define the marked value defined above. For that, we need to add a field that contains the sum of the 'value' elements that were displayed until that moment. This is done using a variable for the current position and the XPath "sum" function.

The xpath that calculates the sum should look like this: xpath="sum(../balance[position() &lt;= $pos]/value)"
The entire code, should look like below:

<fo:table-cell >
   <fo:block>
     <xfd:field xpath="value" format="currency:#,##0.0$ th:, dec:."/>
     <xsl:variable name="pos" select="position()"/>
     <fo:marker marker-class-name="TABLE_FOOTER_SUBTOTAL">
      <xfd:field xpath="sum(../balance[position() &lt;= $pos]/value)" format="currency:#,##0.0$ th:, dec:."/>
     </fo:marker>
   </fo:block>
</fo:table-cell>

Documentation:

- https://www.w3.org/TR/xsl/#fo_marker
- https://www.w3.org/TR/xsl/#fo_retrieve-table-marker

Have more questions? Submit a request

Comments

Powered by Zendesk