RE: [xsl] sum() Function Used With Variables

Subject: RE: [xsl] sum() Function Used With Variables
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 14 Mar 2007 22:13:42 -0000
The problem of summing over computed values is a general one. It's a
one-liner in 2.0 because you can compute a sequence of numbers and sum over
those numbers, for example

sum(for $x in ABCD return number(substring-after($x, '#')))

But in 1.0 you can only sum over nodes, and without the node-set extension
you can't even create new nodes and sum over them. So the solutions tend to
be one of:

1. Two-phase transformation: first write the tree with the computed numbers,
then sum over the nodes in that tree.

2. Ditto within a single stylesheet using the xx:node-set() extension.

3. Don't use sum() at all, do a recursive scan of the values to be summed
and total them "by hand" as you go

4. Use a vendor extension function

5. Use FXSL (which is doing 3, but the hard work has been done for you in
the FXSL library)

Your solution fails because $colwidths is a single node whose string value
is all the column widths concatenated together, and sum('2222') doesn't give
you 8.

Michael Kay

> -----Original Message-----
> From: Ryan Lubben [mailto:ryanl@xxxxxxxxxxx]
> Sent: 14 March 2007 18:15
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] sum() Function Used With Variables
> Hi All,
> I am currently attempting to use the sum() function in XSLT
> 1.0 to total up various table column widths in order to
> determine whether or not the table needs to span the entire
> width of the page. Unfortunately, the column width attribute
> that exists in the XML has the inches designator included in
> the width value.  I have set my stylesheet up so that the
> 'in' designator is filtered through another variable using
> the substring-before() function, but as soon as I attempt to
> use the sum() function with the first variable my stylesheet
> bottoms out.  Any help would be greatly appreciated.
> <xsl:param name="colwidths">
> 	<xsl:choose>
> 		<xsl:when
> test="contains(tgroup/colspec/@colwidth, 'in')">
> 			<xsl:value-of
> select="substring-before(tgroup/colspec/@colwidth,'in')"/>
> 		</xsl:when>
> 		<xsl:otherwise>0</xsl:otherwise>
> 	</xsl:choose>
> </xsl:param>
> <xsl:param name="sum.colwidths" select="sum($colwidths)"/>
> Thanks in advance.
> Ryan Lubben

Current Thread