276 11 The XML Transformation Language
The procedure is called as follows:
<xsl:call-template name="BindingStrengthAverage"/>
Procedures often have parameters, and these are specified in XSLT by us-
ingxsl:paramin the procedure. For example, the following will compute
the average of any set of elements:
<xsl:template name="average">
<xsl:param name="elements"/>
<xsl:value-of
select="sum($elements) div count($elements)"/>
</xsl:template>
When a procedure is called, the parameters are specified using the
xsl:with-paramcommand as follows:
<xsl:call-template name="average">
<xsl:with-param name="elements"
select="BindingStrength"/>
</xsl:call-template>
In general, a computation procedure consists of the following parts:
- The procedure declaration. This consists of the name of the procedure and
the names of the parameters. - The procedure body. This is the part that performs the actual computa-
tion. It usually consists of a conditional element having two parts:
(a) The computation performed on each subelement
(b) The computation performed after all subelements have been processed
The previous example shows the computation of the average, so it is natural
to consider how one might compute the variance using XSLT. The first step
is writing the procedure declaration. In this case there are three relevant
parameters. The first is the set of elements whose variance is to be computed.
The second is theaccumulator. It is the variable that is used for computing
the sum of squares of the elements. It is called the accumulator because it
accumulates the sum by successively adding terms until the entire sum has
been computed. The last parameter is theiterator. Its purpose is to indicate
which term is to be added to the accumulator. Here is the declaration for a
procedure to compute the variance: