Re: [xsl] Subtotals by page

Subject: Re: [xsl] Subtotals by page
From: James Fuller <jim.fuller@xxxxxxxxxxxxxx>
Date: Tue, 15 Mar 2005 12:20:07 +0100
Now what I actually want is the page total on this
page: which would be the value I get when I subtract
the above two values like this:
pagetotal - partsum
In XSL-FO it is not possible to do this.

So my question is even if I do know the number of rows
how do I make it sum that many number of rows?



instead of struggling with your XSLT, why not make your XML easier to work with.....


take the following example xml which represents an simple order

EXAMPLE XML
<?xml version="1.0" encoding="UTF-8"?>
<order>
   <item name="a" price="1.00"/>
   <item name="b" price="1.00"/>
   <item name="c" price="2.00"/>
   <item name="d" price="1.00"/>
   <item name="e" price="2.00"/>
   <item name="f" price="1.00"/>
   <item name="g" price="1.00"/>
   <item name="h" price="1.00"/>
   <item name="i" price="2.00"/>
   <item name="j" price="1.00"/>
   <item name="k" price="2.00"/>
   <item name="l" price="1.00"/>
</order>

wouldnt it be easier to supply your XSLT with something like;

DESIRED XML
<?xml version="1.0" encoding="utf-8"?>
<page>
  <item name="a" price="1.00"/>
  <item name="b" price="1.00"/>
  <item name="c" price="2.00"/>
  <item name="d" price="1.00"/>
  <item name="e" price="2.00"/>
</page>
<page>
  <item name="f" price="1.00"/>
  <item name="g" price="1.00"/>
  <item name="h" price="1.00"/>
  <item name="i" price="2.00"/>
  <item name="j" price="1.00"/>
</page>
<page>
  <item name="k" price="2.00"/>
  <item name="l" price="1.00"/>
</page>

the XML has segreated 5 <item/>'s per <page/> element

there are a variety of ways to achieve this type of grouping, the following is one way using mod and position() and xsl:for-each

EXAMPLE XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:output method="xml"/>
<xsl:template match="order">
<xsl:for-each select="item[position() mod 5 = 1]">
<page>
<xsl:for-each select=".|following-sibling::item[position() &lt;5]">
<xsl:copy-of select="."/>
</xsl:for-each>
</page>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


by having the first stage of XSLT processing massage your XML into a paged format makes the 2nd stage much easier to perform your sum() operations to obtain subtotal and total sums.

gl, Jim Fuller

Current Thread