[xsl] Re: checkbook application -- How to SUM attributes on several nodes based on an attribute in another node ?

Subject: [xsl] Re: checkbook application -- How to SUM attributes on several nodes based on an attribute in another node ?
From: Michael Fourneau <Michael.Fourneau@xxxxxxx>
Date: Wed, 27 Sep 2006 13:26:49 +0200
Hi all,

Got it... it was simpler than what I was trying to do...

<td class="amount-bold">
<xsl:value-of
select="format-number(
sum(/checkbook/payment[ description/@category = $this ]/payor/@amount),
'#.###,00','EUR'
)"
/>
</td>


Let me know if there is a better way to do it :-)

Cheers,
Michael

Michael.Fourneau@xxxxxxx wrote:

=== Posted to xsl-list@xxxxxxxxxxxxxxxxxxxxxx on Wed Sep 27 02:11:42 2006 CEST ===


Hi all,


I'd like to sum the "amount" attribute assigned to each "payor" nodes having the same category for the "category" attribute in the "description" node. Both the "payor" and "description" nodes are children of the "payment" node. You can have several "payor" nodes for a single "payment" node.

After a couple of hours, I can't seem to reach any result...

Thanks beforehand for any help you could bring here.


In my XSLT stylesheet, I have defined a "categories" key to identify all categories used in the XML document:


<!-- KEY -->

<xsl:key name="categories" match="/checkbook/payment/description/@category" use="."/>


Then, I'd like to create a table with the computed amount for each category:


<!-- TABLE -->

<table> <tr> <th>Category</th> <th>Total amount</th> </tr> <xsl:for-each select="/checkbook/payment/description/@category [generate-id(.)=generate-id(key('categories',.))]"> <xsl:sort select="."/> <xsl:param name="this" select="."/> <tr> <td> <xsl:value-of select="$this"/> </td> <td> <xsl:variable name="amount" select="0"/> <xsl:for-each select="/checkbook/payment/description[ @category = $this ]"> <!-- Here I'd like to compute the sum of all amounts available in this category...

??? $amount = $amount + sum(preceding-sibling::payor/@amount) ???

--> </xsl:for-each> <xsl:value-of select="format-number( $amount, '#.###,00','EUR' )" /> </td> </tr> </xsl:for-each> </table>


==== The expected results for the following XML example should be ====


Category Total amount ----------------------------- architecte 500 entrepreneur 1500


==== Example of XML data ===


<checkbook>

<payment type="cash" status="approved"> <payee> Payee X </payee> <payor amount="500"> Payor M </payor> <date>2003-09-21</date> <description category="entrepreneur"> Description A </description> </payment>

<payment type="cash" status="approved"> <payee> Payee Y </payee> <payor amount="250"> Payor C </payor> <payor amount="750"> Payor M </payor> <date>2003-09-22</date> <description category="entrepreneur"> Description B </description> </payment>

<payment type="cash" status="approved"> <payee> Payee Z </payee> <payor amount="100"> Payor C </payor> <payor amount="400"> Payor M </payor> <date>2003-10-22</date> <description category="architecte"> Description B </description> </payment>


</checkbook>





==== DOCUMENT TYPE DEFINITION ===


<?xml version="1.0" encoding="iso-8859-1" ?>

<!-- ***** Document Type Definition (DTD) ***** -->

<!-- Parameter entities -->

<!ENTITY % basic.content '#PCDATA'> <!ENTITY % entry.content 'date, description?, comment?'>


<!ATTLIST checkbook id CDATA #REQUIRED version CDATA #REQUIRED date CDATA #REQUIRED



<!ELEMENT deposit (payee?, payor, %entry.content; )>


<!ATTLIST deposit id ID #IMPLIED type (cash | check | compte | transfert) #REQUIRED ref CDATA #IMPLIED status (approved | pending | rejected) 'pending'


<!ELEMENT payment (payee?, payor+, %entry.content; )>


<!ATTLIST payment id ID #IMPLIED type (cash | check | compte | transfert | bancontact ) #REQUIRED ref CDATA #IMPLIED status (approved | pending | rejected) "pending"


<!-- Basic elements -->


<!ELEMENT date (%basic.content;)*> <!ELEMENT payee (%basic.content;)*> <!ELEMENT payor (%basic.content;)*> <!ATTLIST payor amount CDATA #REQUIRED> <!ELEMENT description (%basic.content;)*> <!ATTLIST description category (achat-maison | vente-maison | materiaux | charges | prime | entrepreneur | architecte | admin | impots-taxes | assurance | autre ) 'autre'> <!ELEMENT comment (%basic.content;)*>




Thanks beforehand,
Michael Fourneau



--
Michael FOURNEAU


Proactive Services

Sun Microsystems Belgium

 Lozengerg 15
 Zaventem 1932 Belgium
 Phone +32-(0)2-704 81 00
 Mobile +32-(0)475-249 510
 Fax +32-(0)2-704 80 80
 Mailto:Michael.Fourneau@xxxxxxx

Current Thread