« Viewing Audits for Stale instances | Main | Invoking an EJB Session Bean from a BPEL Process »

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 (1)

Raghuram:

good content...

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About This Entry

This page contains a single entry from the blog posted on May 7, 2008 9:03 PM.

The previous post in this blog was Viewing Audits for Stale instances.

The next post in this blog is Invoking an EJB Session Bean from a BPEL Process.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type and Oracle