Interesting XSLT Recipe - Computing line totals

Ramkumar Menon
Director, Product Strategy

You might have run into a requirement where you need to compute the total value of all items in a purchase order. Consider the PO XML shown below.

<?xml version="1.0" encoding="UTF-8" ?>  
<po xmlns="http://www.example.org">  

You might want to compute the total value of all items in the purchase Order. For computing this, you would need to multiply the price and qty for each item, and add them up. In XSLT 1.0, I guess the way to do this would be to recursively invoke a template that computes the sum total. In XSLT 2.0, this can be done in a very elegant single expression.


<xsl:variable name="itemVar" select="/tns:po/tns:Items/tns:item"/>                
  <xsl:template match="/">  
      <tns:totalValue><xsl:value-of select="sum(for $i in $itemVar return ($i/tns:price * $i/tns:qty))"/></tns:totalValue>  

Join the discussion

Comments ( 8 )
  • Raghuram Monday, August 11, 2008
    good content...
  • house design perth Tuesday, April 26, 2011
    house design perth
  • craigslist marketing software Thursday, April 28, 2011
    I agree alongside with your put up. Nonetheless, do you may possibly have any resources I can cite for my paper?
  • automated link building Thursday, April 28, 2011
    We are a group of volunteers and starting a new initiative in our community. Your publish furnished us with precious data to assist us get started
  • craigslist posting software Thursday, April 28, 2011
    Hi. I wanted to decline you a rapid word to express my thanks. Ive been following your weblog for a month or so and possess acquired a ton of good data and cherished the method youve structured your site. I am trying to run my quite own weblog however I experience its too basic and I need to give attention to a number of more compact topics. Being everything to all folks is simply not all that its cracked up to be
  • Super Mario Games Thursday, April 28, 2011
    I am so happy I finally observed a particular person different who shares my views and opinions. It is excellent to realize which you merely aren't weird.
  • online casino Thursday, April 28, 2011
    These designs are actually cool. I prefer it submit Keep it up dude :)
  • casino online Thursday, April 28, 2011
    Now you have an enjoyable page that I was happy with
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.