[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Sum produces NaN

Subject: Re: Sum produces NaN
From: "cking" <cking@xxxxxxxxxx>
Date: Mon, 13 Sep 2004 20:24:51 +0200
sum returns nan
Hi James,

I'm not sure why you use named templates, can't you just say

<xsl:value-of select="sum(a/b/data)"/> ?

That would give you the sum of all <data> records,
but indeed returns NaN if one of them is empty.

To solve this, you can narrow your selection to those
<data> elements whose content is not empty:

<xsl:value-of select="sum(a/b/data[.!=''])"/> or
<xsl:value-of select="sum(a/b/data[text()])"/>

That should return "8" for your input

HTH
Anton Triest


James Steven wrote:
>

> Hello
> To add a column of numbers I am using the xsl below.  However if there is
> one value missing in the column then the xsl produces NaN.
>
> eg. Below is xml with a value missing in last <data>.  I then use the xsl
> below to add all <data> values but receive NaN because of the missing value.
> If all <data> tags have value then the sum works.  Please could anyone show
> how to resolve this.  Any help greatly appreciated.
>
> <a>
>   <b>
> <data>1</data>
>   </b>
>   <b>
>       <data>2</data>
>   </b>
>   <b>
>       <data>5</data>
>   </b>
>   <b>
>       <data></data>
>   </b>
> </a>
>
>
> <xsl:template name="orderlist.sum.edit">
> <xsl:param name="set-of-order"/>
> <xsl:variable name="sumorg">
>   <xsl:call-template name="orderlist.sum">
>       <xsl:with-param name="set-of-order" select="$set-of-order"/>
>   </xsl:call-template>
>   </xsl:variable>
> <xsl:value-of select='format-number($sumorg, "####,###,##0.00")'/>
> </xsl:template>
>
> <xsl:template name="orderlist.sum">
> <xsl:param name="set-of-order"/>
> <xsl:choose>
>   <xsl:when test="$set-of-order">
>   <xsl:variable name="first">
>   <xsl:apply-templates select="$set-of-order[1]/data"/>
>   </xsl:variable>
>   <xsl:variable name="rest">
>   <xsl:call-template name="orderlist.sum">
>   <xsl:with-param name="set-of-order" select="$set-of-order[position()
> != 1]"/>
>   </xsl:call-template>
>   </xsl:variable>
>   <xsl:value-of select="$first + $rest"/>
>   </xsl:when>
>   <xsl:otherwise>0</xsl:otherwise>
>   </xsl:choose>
>   </xsl:template>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.