• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Group & Sum Using XSLT

 
Ranch Hand
Posts: 290
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a page on various grouping techniques in XSLT.
 
Aryan Khan
Ranch Hand
Posts: 290
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't that just something like this?
 
Aryan Khan
Ranch Hand
Posts: 290
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic