untitled

(ff) #1

278 11 The XML Transformation Language


<xsl:call-template name="variance">
<xsl:with-param name="elements" select="$elements"/>
<xsl:with-param name="ssq"
select="$ssq + $elements[position()=$i] *
$elements[position()=$i]"/>
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>

The firstxsl:with-paramcommand adds the square of the next element
to the accumulator. The second command increases the iterator by 1. The
call to the procedure continues the computation. The two commands can
be written in either order as they take effect only after the computation is
continued. So the following program does the same computation:

<xsl:call-template name="variance">
<xsl:with-param name="i" select="$i + 1"/>
<xsl:with-param name="ssq"
select="$ssq + $elements[position()=$i] *
$elements[position()=$i]"/>
<xsl:with-param name="elements" select="$elements"/>
</xsl:call-template>

The final computation divides the sum of squares by the number of ele-
ments and subtracts the square of the average:

<xsl:variable name="avg"
select="sum($elements) div count($elements)"/>
<xsl:value-of
select="$ssq div count($elements) - $avg * $avg"/>

Putting these together gives the following procedure for computing the
variance:
<xsl:template name="variance">
<xsl:param name="elements"/>
<xsl:param name="ssq"/>
<xsl:param name="i"/>
<xsl:choose>
<xsl:when test="$i > count($elements)">
<xsl:variable name="avg"
select="sum($elements) div count($elements)"/>
<xsl:value-of
select="$ssq div count($elements) - $avg * $avg"/>
Free download pdf