O'Reilly logo

XSLT Cookbook by Sal Mangano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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']"> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required