[xsl] Weird variable scoping rules

Subject: [xsl] Weird variable scoping rules
From: Graham Ashton <gashton@xxxxxxxxxxx>
Date: 15 May 2002 17:21:02 +0100
If you take the following snippet of markup, how would a template that
processed a cell element work out the *maximum* number of <label>
elements in any of the <row>'s <cell> elements?

          <label value="foo"/>
          <label value="bar"/>
          <label value="baz"/>

I tried to iterate over the cells and store the number of labels in a
"variable" (i.e. a param), keeping track of the maximum number seen for
any cell, but my attempts to keep track of the maximum value seen so far
aren't working.

Here's a template that runs on a single cell:

<xsl:template match="cell">

  <xsl:param name="max_vgroup_size" select="0"/>

  <xsl:for-each select="../cell">
    <xsl:param name="vgroup_size" select="count(vgroup/*)"/>
    <xsl:if test="$vgroup_size > $max_vgroup_size">
      <xsl:param name="max_vgroup_size" select="$vgroup_size"/>

    vgroup: <xsl:value-of select="$max_vgroup_size"/>


The assignment to $max_vgroup_size in the <xsl:if/> block works fine,
but by the time I want to use it (in the <td> at the bottom) the
$max_vgroup_size variable defined in the <xsl:if/> block has gone out of

How should I be approaching this? Thanks....

Graham Ashton

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread