• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Group & Sum Using XSLT

 
Aryan Khan
Ranch Hand
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a page on various grouping techniques in XSLT.
 
Aryan Khan
Ranch Hand
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that just something like this?
 
Aryan Khan
Ranch Hand
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic