XML technologies / XSLT / XSLT Cookbook / Numbers and calculations / Calculate sums and products
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You need to sum or multiply functions of numbers contained in a node set.
Solution
XSLT 1.0
With tail-recursion optimization, the abstract form of a sum is:
<xsl:template name="ckbk:sum">
<!-- Knoten auf eine leere Knotenmenge initialisieren -->
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="0"/>
<xsl:choose>
<xsl:when test="not($nodes)">
<xsl:value-of select="$result"/>
</xsl:when>
<xsl:otherwise>
<!-- Template aufrufen oder anwenden, das den Wert des Knotens ermittelt, es sei denn,der Knoten ist der tatsächliche Wert, der summiert werden soll -->
<xsl:variable name="value">
<xsl:call-template name="some-function-of-a-node">
<xsl:with-param name="node" select="$nodes[1]"/>
</xsl:call-template>
</xsl:variable>
<!-- Rekursion, um den Rest zu summieren -->
<xsl:call-template name="ckbk:sum">
<xsl:with-param name="nodes" select="$nodes[position( ) != 1]"/>
<xsl:with-param name="result" select="$result + $value"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>Without it, two techniques handle many nodes. The first is divide and conquer, reducing the work by at least half at each recursive step:
<xsl:template name="ckbk:sum-dvc">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="0"/>
<xsl:param name="dvc-threshold" select="100"/>
<xsl:choose>
<xsl:when test="count($nodes) <= $dvc-threshold">
<xsl:call-template name="ckbk:sum">
<xsl:with-param name="nodes" select="$nodes"/>
<xsl:with-param name="result" select="$result"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="half" select="floor(count($nodes) div 2)"/>
<xsl:variable name="sum1">
<xsl:call-template name="ckbk:sum-dvc">
<xsl:with-param name="nodes" select="$nodes[position( ) <= $half]"/>
<xsl:with-param name="result" select="$result"/>
<xsl:with-param name="dvc-threshold" select="$dvc-threshold"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="ckbk:sum-dvc">
<xsl:with-param name="nodes" select="$nodes[position( ) > $half]"/>
<xsl:with-param name="result" select="$sum1"/>
<xsl:with-param name="dvc-threshold" select="$dvc-threshold"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>The second, batching, has two recursion levels: divide the large problem into manageable batches, then recursively process each batch:
<xsl:template name="ckbk:sum-batcher">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="0"/>
<xsl:param name="batch-size" select="500"/>
<xsl:choose>
<xsl:when test="not($nodes)">
<xsl:value-of select="$result"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="batch-sum">
<xsl:call-template name="ckbk:sum">
<xsl:with-param name="nodes" select="$nodes[position( ) < $batch-size]"/>
<xsl:with-param name="result" select="$result"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="ckbk:sum-batcher">
<xsl:with-param name="nodes" select="$nodes[position( ) >= $batch-size]"/>
<xsl:with-param name="result" select="$batch-sum"/>
<xsl:with-param name="batch-size" select="$batch-size"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>Solutions for product are similar:
<xsl:template name="ckbk:product">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="1"/>
<xsl:choose>
<xsl:when test="not($nodes)">
<xsl:value-of select="$result"/>
</xsl:when>
<xsl:otherwise>
<!-- Template aufrufen oder anwenden, das den Wert des Knotens ermittelt, es sei denn, der Knoten ist tatsächlich der Wert, der multipliziert werden soll -->
<xsl:variable name="value">
<xsl:call-template name="some-function-of-a-node">
<xsl:with-param name="node" select="$nodes[1]"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="ckbk:product">
<xsl:with-param name="nodes" select="$nodes[position( ) != 1]"/>
<xsl:with-param name="result" select="$result * $value"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ckbk:product-batcher">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="1"/>
<xsl:param name="batch-size" select="500"/>
<xsl:choose>
<xsl:when test="not($nodes)">
<xsl:value-of select="$result"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="batch-product">
<xsl:call-template name="ckbk:product">
<xsl:with-param name="nodes" select="$nodes[position( ) < $batch-size]"/>
<xsl:with-param name="result" select="$result"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="ckbk:product-batcher">
<xsl:with-param name="nodes" select="$nodes[position( ) >= $batch-size]"/>
<xsl:with-param name="result" select="$batch-product"/>
<xsl:with-param name="batch-size" select="$batch-size"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ckbk:product-dvc">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="1"/>
<xsl:param name="dvc-threshold" select="100"/>
<xsl:choose>
<xsl:when test="count($nodes) <= $dvc-threshold">
<xsl:call-template name="ckbk:product">
<xsl:with-param name="nodes" select="$nodes"/>
<xsl:with-param name="result" select="$result"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="half" select="floor(count($nodes) div 2)"/>
<xsl:variable name="product1">
<xsl:call-template name="ckbk:product-dvc">
<xsl:with-param name="nodes" select="$nodes[position( ) <= $half]"/>
<xsl:with-param name="result" select="$result"/>
<xsl:with-param name="dvc-threshold" select="$dvc-threshold"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="ckbk:product-dvc">
<xsl:with-param name="nodes" select="$nodes[position( ) > $half]"/>
<xsl:with-param name="result" select="$product1"/>
<xsl:with-param name="dvc-threshold" select="$dvc-threshold"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>Note:
The built-in XPath function sum( ) is the simplest way to compute ordinary sums. For sums of arbitrary functions of nodes, either
use a recipe from this section, or
first compute the function and store the result as a result tree fragment in a variable. Convert it to a node set with an extension function and pass it to
sum. In XSLT 2.0, generalized sums are trivial because result tree fragments are no longer allowed.
XSLT 2.0
Sequences, XPath’s for expression and sum function make arbitrary sums trivial in 2.0:
<!-- Summe von Quadraten -->
<xsl:value select="sum(for $i in $nodes return $i * $i)"/>XPath has no native prod( ) function, so provide your own:
<xsl:function name="ckbk:prod" as="xs:double">
<xsl:param name="numbers" as="xs:double*"/>
<xsl:sequence select="if (count($numbers) eq 0) then 0 else if (count($numbers) = 1) then $numbers[1] else $numbers[1] * ckbk:prod(subsequence($numbers,2))"/>
</xsl:function>Discussion
XSLT 1.0
Batching and divide and conquer manage recursion when processing potentially large node sets. Experiments show better performance even with an XSLT processor recognizing tail recursion.
Tip: Extend and embed XSLT shows reusable batching and divide-and-conquer drivers.
XSLT 2.0
You may want to compute factorials with this product function:
<xsl:function name="ckbk:factorial" as="xs:double">
<xsl:param name="n" as="xs:integer"/>
<xsl:sequence select="if ($n eq 0) then 1 else ckbk:prod(1 to $n)"/>
</xsl:function>This is probably acceptable when $n is small. For large values, constructing the sequence may be slower than a direct solution. Keep this in mind when exploiting sequences’ convenience. Michael Kay gives more examples comparing his Knights Tour 1.0 and 2.0 solutions in XSLT 2.0, third edition, Wrox, 2004, p. 753:
//was tut dieser Code?
<xsl:function name="ckbk:factorial" as="xs:decimal">
<xsl:param name="n" as="xs:integer"/>
<xsl:sequence select="if ($n eq 0) then 1 else $n * ckbk:factorial($n - 1)"/>
</xsl:function>
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de