Interesting XSLT Recipe - Computing line totals

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">  
   <Items>  
      <item>  
         <price>1</price>  
         <qty>2</qty>  
      </item>  
      <item>  
         <price>3</price>  
         <qty>4</qty>  
      </item>  
      <item>  
         <price>5</price>  
         <qty>6</qty>  
      </item>  
   </Items>  
</po> 

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:poSummary>  
      <tns:totalValue><xsl:value-of select="sum(for $i in $itemVar return ($i/tns:price * $i/tns:qty))"/></tns:totalValue>  
    </tns:poSummary>  
  </xsl:template>  
</xsl:stylesheet>  

Comments:

good content...

Posted by Raghuram on August 10, 2008 at 10:56 PM PDT #

house design perth

Posted by house design perth on April 25, 2011 at 10:12 PM PDT #

I agree alongside with your put up. Nonetheless, do you may possibly have any resources I can cite for my paper?

Posted by craigslist marketing software on April 27, 2011 at 07:20 PM PDT #

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

Posted by automated link building on April 27, 2011 at 08:05 PM PDT #

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

Posted by craigslist posting software on April 27, 2011 at 09:26 PM PDT #

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.

Posted by Super Mario Games on April 28, 2011 at 01:07 AM PDT #

These designs are actually cool. I prefer it submit Keep it up dude :)

Posted by online casino on April 28, 2011 at 03:25 AM PDT #

Now you have an enjoyable page that I was happy with

Posted by casino online on April 28, 2011 at 03:37 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Principal Product Manager

Search

Archives
« March 2015
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today