Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2006, 05:24 PM
Registered User
 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grouping - Summing - Report Generation

Hi,
I try to generate a report from a database export of some financial transactions, using C++ would have been easy but I try the funny way.

(all the files are here : http://hylvenir.free.fr/data/xslt/ )

Here my XML :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<TABLE>
    <ROW><TYP>F</TYP><SEN>R</SEN><BIL>1</BIL><TRA> 1</TRA><QTE>5000</QTE></ROW>
    <ROW><TYP>F</TYP><SEN>R</SEN><BIL>1</BIL><TRA> 2</TRA><QTE>-4000</QTE></ROW>
    <ROW><TYP>F</TYP><SEN>R</SEN><BIL>1</BIL><TRA> 7</TRA><QTE>5000</QTE></ROW>
    <ROW><TYP>F</TYP><SEN>R</SEN><BIL>1</BIL><TRA> 8</TRA><QTE>-4500</QTE></ROW>
    <ROW><TYP>F</TYP><SEN>P</SEN><BIL>2</BIL><TRA> 3</TRA><QTE>2000</QTE></ROW>
    <ROW><TYP>F</TYP><SEN>P</SEN><BIL>2</BIL><TRA> 9</TRA><QTE>2000</QTE></ROW>
    <ROW><TYP>I</TYP><SEN>R</SEN><BIL>3</BIL><TRA> 4</TRA><QTE>3000</QTE></ROW>
    <ROW><TYP>I</TYP><SEN>R</SEN><BIL>3</BIL><TRA> 5</TRA><QTE>1500</QTE></ROW>
    <ROW><TYP>I</TYP><SEN>R</SEN><BIL>3</BIL><TRA>10</TRA><QTE>3000</QTE></ROW>
    <ROW><TYP>I</TYP><SEN>R</SEN><BIL>3</BIL><TRA>11</TRA><QTE>1500</QTE></ROW>
    <ROW><TYP>I</TYP><SEN>P</SEN><BIL>3</BIL><TRA> 6</TRA><QTE>2000</QTE></ROW>
    <ROW><TYP>I</TYP><SEN>P</SEN><BIL>3</BIL><TRA>12</TRA><QTE>2000</QTE></ROW>
</TABLE>
Here my XSL:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
<xsl:output method="xml" indent="yes"/>

  <xsl:template match="TABLE">
    <table border='1'>
      <tr><th>TYP</th><th>SEN</th><th>QTE</th></tr>
      <xsl:for-each-group select="ROW" group-by="TYP">
        <tr><td><xsl:value-of select="current-grouping-key()" /></td>
        <xsl:for-each-group select="current-group()" group-by="SEN">
            <td><xsl:value-of select="current-grouping-key()" /></td>
            <td><xsl:value-of select="sum(current-group()/QTE)" /></td>
        </xsl:for-each-group>
        </tr>
      </xsl:for-each-group>
    </table>

    <br />

    <table border='0' cellpadding='0' cellspacing='0'>
      <tr><th>TYP</th><th>SEN</th><th>BIL</th><th>TRA</th><th>QTE</th></tr>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="ROW">
      <xsl:if test="count(preceding-sibling::*) = 0 or preceding-sibling::*[position()=1]/TYP != TYP">
        <tr><td colspan='5'></td></tr>
      </xsl:if>
      <tr>
          <td>
            <xsl:if test="count(preceding-sibling::*) = 0 or preceding-sibling::*[position()=1]/TYP != TYP">
                <xsl:value-of select="TYP" />
            </xsl:if>
          </td>
          <td>
            <xsl:if test="count(preceding-sibling::*) = 0
                          or preceding-sibling::*[position()=1]/TYP != TYP
                          or preceding-sibling::*[position()=1]/SEN != SEN">
              <xsl:value-of select="SEN" />
            </xsl:if>
          </td>
          <td><xsl:value-of select="BIL" /></td>
          <td><xsl:value-of select="TRA" /></td>
          <td style='text-align:right;'><xsl:value-of select="format-number( QTE, '#,###.00' )" /></td>
      </tr>

      <xsl:if test="count(following-sibling::*) = 0 
                    or following-sibling::*[position()=1]/BIL != BIL
                    or following-sibling::*[position()=1]/SEN != SEN
                    or following-sibling::*[position()=1]/TYP != TYP">
        <tr>
           <td colspan='3' /><td>Total : </td>
           <td style='text-align:right;'><xsl:value-of select="format-number( sum( preceding-sibling::*[TYP = ./TYP]/QTE ) + QTE, '#,###.00'  )" /></td>
        </tr>
      </xsl:if>

      <xsl:if test="count(following-sibling::*) = 0 
                    or following-sibling::*[position()=1]/TYP != TYP">
        <tr><td colspan='5'></td></tr>
        <tr>
           <td colspan='3' /><th>Total : </th>
           <td style='text-align:right;'><xsl:value-of select="format-number( sum( preceding-sibling::*[TYP = ./TYP]/QTE ) + QTE, '#,###.00'  )" /></td>
        </tr>
      </xsl:if>
  </xsl:template>


</xsl:stylesheet>
I have put what I got (using Saxon8.7) on the link (sup2.html)
There are some things wrong:
the totals are wrong and I don't succeed to correct.
I don't succeed to make a correct summary by TYP and SEN (the first table ), each different tuple TYP/SEN on different lines.

Is there a better to generate sum for rupture for this kind of reports ? like generating an intermediaire tree using for-each-group for example ?

Thanks.





 
Old May 6th, 2006, 04:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm not quite sure what you're doing here. You seem to be trying to use two different approaches to grouping - are they supposed to produce the same output or different output? I'll concentrate on the first and ignore the second (which does things the old 1.0 way).

You apparently want to produce new rows for each "secondary" group, but you make no attempt to produce them. A good start would be:

  <xsl:template match="TABLE">
    <table border='1'>
      <tr><th>TYP</th><th>SEN</th><th>QTE</th></tr>
      <xsl:for-each-group select="ROW" group-by="TYP">
        <tr><td colspan="3"><xsl:value-of select="current-grouping-key()" /></td>

        <xsl:for-each-group select="current-group()" group-by="SEN">
        <tr><td/>
            <td><xsl:value-of select="current-grouping-key()" /></td>
            <td><xsl:value-of select="sum(current-group()/QTE)"/></td>
        </xsl:for-each-group>
        </tr>
      </xsl:for-each-group>
    </table>

If you don't want to start a new row for the first group, you need some logic inside the inner for-each-group that does different things depending on test="position()=1". Alternatively, an easier way might be to produce nested tables, though you will have to be careful with the CSS formatting if you want everything nicely aligned.

You also say the totals are wrong but they look right to me.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 6th, 2006, 05:22 AM
Registered User
 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

My report had to have two parts : a summarize and details

the first table is just a summarize of the second one (details).
The sums are good with in the first but wrong in the second.

My first attempt was using nested tables, but can it work with xslt-fo too ?

I will try to improve my stylesheet with your help.
Thanks.

 
Old May 6th, 2006, 07:34 AM
Registered User
 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here what I have done with nested tables (I am not sure it will works with xslt-fo)
I am near from what I want.
I have put my stylesheet on the previous url and the result too.
I not sure I like the duplicate code between the summary and the detail.
I will use CSS too make the alignement.
The next step will be to cut the detail table in 4 tables (I have seen something in the FAQ about that).


Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
<xsl:output method="xml" indent="yes"/>

  <xsl:template match="TABLE">

    <table border='1'>
      <tr><th colspan='2'>Report Summary</th></tr>
      <tr>
        <td>
          <xsl:for-each-group select="ROW" group-by="TYP">
            <table border='1'>
              <tr>
                <td><xsl:value-of select="current-grouping-key()" /></td>
                <td>
                   <xsl:for-each-group select="current-group()" group-by="SEN">
                     <table  border='1'>
                       <tr>
                         <td><xsl:value-of select="current-grouping-key()" /></td>
                         <td><xsl:value-of select="sum( current-group()/QTE )" /></td></tr>
                     </table> 
                   </xsl:for-each-group>
                </td>
              </tr>
              <tr><td>Total : </td><td><xsl:value-of select="sum( current-group()/QTE )" /></td></tr>
            </table>
          </xsl:for-each-group>
        </td>
      </tr>
    </table>

    <br />
    <table border='1'>
      <tr><th colspan='3'>Report Detail</th></tr>
      <tr>
        <td>
          <xsl:for-each-group select="ROW" group-by="TYP">
            <table border='1'>
              <tr>
                <td><xsl:value-of select="current-grouping-key()" /></td>
                <td>
                   <xsl:for-each-group select="current-group()" group-by="SEN">
                     <table  border='1'>
                       <tr>
                         <td><xsl:value-of select="current-grouping-key()" /></td>
                         <td>

                           <table border='1'>
                             <xsl:for-each select="current-group()" >
                               <tr>
                                 <td><xsl:value-of select="BIL" /></td>
                                <td><xsl:value-of select="TRA" /></td>
                                <td><xsl:value-of select="QTE" /></td>
                               </tr>
                             </xsl:for-each>
                           </table>

                        </td>
                       </tr>
                       <tr><td>Total : </td><td><xsl:value-of select="sum( current-group()/QTE )" /></td></tr>
                     </table> 
                   </xsl:for-each-group>
                </td>
              </tr>
              <tr><td>Total : </td><td><xsl:value-of select="sum( current-group()/QTE )" /></td></tr>
            </table>
          </xsl:for-each-group>
        </td>
      </tr>
    </table>

  </xsl:template >
</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Report Generation kotaiah Excel VBA 0 September 14th, 2006 08:26 AM
Report Generation Application jmss66 VB How-To 2 February 9th, 2006 01:31 PM
Help with summing calculated fields on a report dbartelt Access 6 January 4th, 2006 03:33 PM
Report Generation in JAVA Bhushan XML 0 May 18th, 2005 05:26 AM
Report generation question pankaj_daga Access 5 December 21st, 2003 06:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.