xsl-list
[Top] [All Lists]

RE: Sum produces NaN

2004-09-13 11:41:56
Firstly, why are you doing this with a recursive template rather than by
using the sum() function?

Secondly, why are you doing this:

<xsl:variable name="first">
  <xsl:apply-templates select="$set-of-order[1]/data"/>
</xsl:variable> 

rather than

<xsl:variable name="first" select="$set-of-order[1]/data"/>

However, whichever way you do it, it's easy enough to include conditional
logic to exclude the empty data elements. For example, you can write:

sum(b/data[.!=''])

Michael Kay
http://www.saxonica.com/





-----Original Message-----
From: James Steven [mailto:JSteven(_at_)redlinesoftware(_dot_)co(_dot_)uk] 
Sent: 13 September 2004 16:01
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Sum produces NaN

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>



--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--





<Prev in Thread] Current Thread [Next in Thread>