Creating Generic Bounded Aggregation Functions

Problem

You want to create reusable templates for performing a wide variety of bounded aggregation operations.

Solution

 <xsl:template name="generic:bounded-aggregation"> <xsl:param name="x" select="0"/> <xsl:param name="func" select=" 'identity' "/> <xsl:param name="func-param1"/> <xsl:param name="test-func" select=" 'less-than' "/> <xsl:param name="test-param1" select="$x + 1"/> <xsl:param name="incr-func" select=" 'incr' "/> <xsl:param name="incr-param1" select="1"/> <xsl:param name="i" select="1"/> <xsl:param name="aggr-func" select=" 'sum' "/> <xsl:param name="aggr-param1"/> <xsl:param name="accum" select="$generic:generics[self::generic:aggr-func and @name = $aggr-func]/@identity"/> <!-- Check if aggregation should continue --> <xsl:variable name="continue"> <xsl:apply-templates select="$generic:generics[self::generic:func and @name = $test-func]"> <xsl:with-param name="x" select="$x"/> <xsl:with-param name="param1" select="$test-param1"/> </xsl:apply-templates> </xsl:variable> <xsl:choose> <xsl:when test="string($continue)"> <!--Compute func($x) --> <xsl:variable name="f-of-x"> <xsl:apply-templates select="$generic:generics[self::generic:func and @name = $func]"> <xsl:with-param name="x" select="$x"/> <xsl:with-param name="i" select="$i"/> <xsl:with-param name="param1" select="$func-param1"/> </xsl:apply-templates> </xsl:variable> <!-- Aggregate current $f-of-x with $accum --> <xsl:variable name="temp"> <xsl:apply-templates ...

Get XSLT Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.