Creating Generic Element Aggregation Functions

Problem

You want to create reusable templates that perform a wide variety of node-set aggregation operations.

Solution

A fully generic extensible solution exploits the template-tagging method discussed in this chapter’s introduction:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:generic="http://www.ora.com/XSLTCookbook/namespaces/generic" xmlns:aggr="http://www.ora.com/XSLTCookbook/namespaces/aggregate" extension-element-prefixes="generic"> <xsl:variable name="generic:public-generics" select="document('')/*/generic:*"/> <xsl:variable name="generic:generics" select="$generic:public-generics"/> <!-- Primitive generic functions on x --> <generic:func name="identity"/> <xsl:template match="generic:func[@name='identity']"> <xsl:param name="x"/> <xsl:value-of select="$x"/> </xsl:template> <generic:func name="square"/> <xsl:template match="generic:func[@name='square']"> <xsl:param name="x"/> <xsl:value-of select="$x * $x"/> </xsl:template> <generic:func name="cube"/> <xsl:template match="generic:func[@name='cube']"> <xsl:param name="x"/> <xsl:value-of select="$x * $x * $x"/> </xsl:template> <generic:func name="incr" param1="1"/> <xsl:template match="generic:func[@name='incr']"> <xsl:param name="x"/> <xsl:param name="param1" select="@param1"/> <xsl:value-of select="$x + $param1"/> </xsl:template> <!-- Primitive generic aggregators --> <generic:aggr-func name="sum" identity="0"/> <xsl:template match="generic:aggr-func[@name='sum']"> ...

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.