This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Weird variable scoping rules
- From: Graham Ashton <gashton at cmedltd dot com>
- To: xsl-list <xsl-list at lists dot mulberrytech dot com>
- Date: 15 May 2002 17:21:02 +0100
- Subject: [xsl] Weird variable scoping rules
- Reply-to: xsl-list at lists dot mulberrytech dot com
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?
<row>
<cell>
<vgroup>
<label value="foo"/>
</vgroup>
</cell>
<cell>
<vgroup>
<label value="bar"/>
<label value="baz"/>
</vgroup>
</cell>
<cell/>
</row>
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"/>
</xsl:if>
</xsl:for-each>
<td>
vgroup: <xsl:value-of select="$max_vgroup_size"/>
<xsl:apply-templates/>
</td>
</xsl:template>
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
scope.
How should I be approaching this? Thanks....
--
Graham Ashton
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list