You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Alain ROY <ra...@email.com> on 2004/07/20 16:28:07 UTC

Calculating subtotals

Hi all,

I made a stylesheet to generate a PDF file. It print table rows with an amount on each row. I'd like to calculate subtotals of these amounts while running through my xml document so that I could print the rows subtotal on the page footer when there is a page break.
Does anybody knows how I could do that ?

Regards
AR
-- 
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm




---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Calculating subtotals

Posted by "J.Pietschmann" <j3...@yahoo.de>.
Alain ROY wrote:
> I made a stylesheet to generate a PDF file. It print table rows with
> an amount on each row. I'd like to calculate subtotals of these
> amounts while running through my xml document so that I could print
> the rows subtotal on the page footer when there is a page break. Does
> anybody knows how I could do that ?

This is more an XSLT question, it comes up now and then
on the XSL list
  http://www.mulberrytech.com/xsl/xsl-list/
and theres's probably already a FAQ entry (reachable from the
URL above).

The common advice is to use markers. Add a marker with the
running subtotal to each table row, and reference the
last marker of the page in the page footer. In FOP 0.20.5,
you must put the marker definition in a block in a table cell,
it wont work if the marker is an immediate child of a table
row.

Here is an old XML+XSLT to get you started:

<?xml version="1.0"?>
<elements>
   <element>
     <name>prod 1</name><price>10.0</price>
   </element>
   <element>
     <name>prod 2</name><price>5.0</price>
   </element>
   <element>
     <name>prod 3</name><price>2.0</price>
   </element>
   <element>
     <name>prod 4</name><price>1.1</price>
   </element>
   <element>
     <name>prod 5</name><price>3.4</price>
   </element>
   <element>
     <name>prod 6</name><price>5.9</price>
   </element>
   <element>
     <name>prod 7</name><price>4.88</price>
   </element>
   <element>
     <name>prod 8</name><price>20.04</price>
   </element>
</elements>

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:fo="http://www.w3.org/1999/XSL/Format">

   <xsl:strip-space elements="*"/>

   <xsl:template match="elements">
     <fo:root>
       <fo:layout-master-set>
         <fo:simple-page-master master-name="simple"
           page-width="6cm" page-height="5cm">
           <fo:region-body margin-bottom="2cm"/>
           <fo:region-after extent="2cm"/>
         </fo:simple-page-master>
       </fo:layout-master-set>
       <fo:page-sequence master-reference="simple" font-size="24pt">
         <fo:static-content flow-name="xsl-region-after">
           <fo:table table-layout="fixed" width="100%">
             <fo:table-column column-width="3cm"/>
             <fo:table-column column-width="3cm"/>
             <fo:table-body>
               <fo:table-row>
                 <fo:table-cell border-before-style="solid"
                   border-before-width="2pt">
                   <fo:block>
                     <fo:retrieve-marker retrieve-class-name="subtotalname"
                       retrieve-boundary="page"
                       retrieve-position="last-starting-within-page"/>
                   </fo:block>
                 </fo:table-cell>
                 <fo:table-cell border-before-style="solid"
                   border-before-width="2pt">
                   <fo:block text-align-last="end">
                     <fo:retrieve-marker retrieve-class-name="subtotalvalue"
                       retrieve-boundary="page"
                       retrieve-position="last-starting-within-page"/>
                   </fo:block>
                 </fo:table-cell>
               </fo:table-row>
             </fo:table-body>
           </fo:table>
         </fo:static-content>
         <fo:flow flow-name="xsl-region-body">
           <fo:table table-layout="fixed" width="100%">
             <fo:table-column column-width="3cm"/>
             <fo:table-column column-width="3cm"/>
             <fo:table-body>
               <xsl:apply-templates select="element"/>
             </fo:table-body>
           </fo:table>
         </fo:flow>
       </fo:page-sequence>
     </fo:root>
   </xsl:template>

   <xsl:template match="element">
     <fo:table-row>
       <fo:table-cell>
         <fo:block>
           <fo:marker marker-class-name="subtotalname">
             <xsl:choose>
               <xsl:when test="position()=last()">
                 <xsl:text>total</xsl:text>
               </xsl:when>
               <xsl:otherwise>
                 <xsl:text>subtotal</xsl:text>
               </xsl:otherwise>
             </xsl:choose>
           </fo:marker>
           <fo:marker marker-class-name="subtotalvalue">
             <xsl:value-of select="price + sum(preceding::price)"/>
           </fo:marker>
           <xsl:value-of select="name"/>
         </fo:block>
       </fo:table-cell>
       <fo:table-cell>
         <fo:block text-align-last="end">
           <xsl:value-of select="price"/>
         </fo:block>
       </fo:table-cell>
     </fo:table-row>
   </xsl:template>

</xsl:stylesheet>

J.Pietschmann

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org