File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Group & Sum Using XSLT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Group & Sum Using XSLT" Watch "Group & Sum Using XSLT" New topic
Author

Group & Sum Using XSLT

Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Hi,
How can I sum the PaymentAmount grouped by PaymentType. So the output for the following xml will be as:
DEBITS: 19200.00
CREDITS: 25567.00
------------------------
<Transaction>
<Payment>
PaymentDetails>
<PaymentType>CREDIT</PaymentType>
<PaymentAmount>19000.00</PaymentAmount>
</PaymentDetails>
<PaymentDetails>
<PaymentType>DEBIT</PaymentType>
<PaymentAmount>12000.00</PaymentAmount>
</PaymentDetails>
<PaymentDetails>
<PaymentType>DEBIT</PaymentType>
<PaymentAmount>13567.00</PaymentAmount>
</PaymentDetails>
<PaymentDetails>
<PaymentType>CREDIT</PaymentType>
<PaymentAmount>200.00</PaymentAmount>
</PaymentDetails>
</Payment>
</Transaction>

Thanks


OCP/MCP/SCJP/SCWCD/IBM XML/SCMAD/SCEA-1
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Here is a page on various grouping techniques in XSLT.
Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Still unable to figure out!
Here is what I want -- Sum of an element based on the value of its sibling.
Requirement:
Check the value of PaymentType ;if Credit, add to a variable, if Debit to some other variable. Variable re-assignment not possible. I am looking into recursion.
Appreciate if you can share you experience/expertise.
Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Isn't that just something like this?
Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Hi Paul,
Thanks alot. It worked perfectly.
I tried the same thing but like:
sum(/Transaction/Payment/PaymentDetails/PaymentAmount[/Transaction/Payment/PaymentDetails/PaymentType='CREDIT'])
Instead of
sum(/Transaction/Payment/PaymentDetails/PaymentAmount[../PaymentType='CREDIT'])
--------
So does this mean that the path in predicates are not relative to template but sort of current context or the xpath they belong to?
Thank you again!
Ahmad
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That's a good question. Yes, relative paths in an XPath expression are relative to the context node. (And not relative to the node the predicate is applied to, as I wrongly implied in my example.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Group & Sum Using XSLT