OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [xml-dev] maths in xml



Gordon Stewart wrote:

> Dear List
> 
> I have a XML file that has the following structure
> 
> <product>
>   <unitprice>2.00</unitprice>
>   <sold>5</sold>
> </product>
> <product>
>   <unitprice>3.00</unitprice>
>   <sold>2</sold>
> </product>
> 
> How do I get XML to display the data so that the xsl file has calculation in
> it so that the data shows as
> 
> 10.00
> 6.00
> 
> and then I would like to add them up so I get 16.00.
> 
> I do not want to hard code the value into my XML document.


Assuming a <products> wrapper around the main elements, the following 
seems to work:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:internal="foo"
                 version='1.0'>
  <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

  <xsl:template match="products">
   <xsl:variable name="individual">
    <xsl:apply-templates />
   </xsl:variable>
   <xsl:value-of select="format-number(sum($individual/internal:mult),
                                       '##.00')"/>
  </xsl:template>

  <xsl:template match="product">
   <internal:mult>
    <xsl:value-of select="number(unitprice * sold)"/>
   </internal:mult>
  </xsl:template>

</xsl:stylesheet>

You could probably do it without using an internal tree (which I think 
may not be standard).

Btw, you should direct questions about xsl to the XSL-List: 
http://www.mulberrytech.com/xsl/xsl-list/

--
Mike