You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Frank Olsen <fo...@acm.org> on 2000/09/07 14:57:00 UTC
Scope of variables in XSL templates (Cocoon, Xalan, ...)
Hi,
I wonder if anyone could please help me with an XSLT problem. For
reference: I use Cocoon 1.7.4 (with the versions of Xalan, Xerces, etc.
furnished with Cocoon ) under WebSphere 3.0.2.1.
I'm trying to find a way to navigate in a result set from a DB query;
i.e., I want to present the results to the user in packets of, say, 10.
I use a Cocoon custom producer to send the query and to send the XML
result set to an XSL stylesheet, the relevant portion of which is given
here:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="common.xsl"/>
<xsl:template match="QueryResults">
<xsl:variable name="numToDisplay"><xsl:value-of
select="../parameters/numToDisplay" /></xsl:variable>
<xsl:variable name="lastNumDisplayed"><xsl:value-of
select="../parameters/lastNumDisplayed" /></xsl:variable>
<xsl:variable name="direction"><xsl:value-of
select="../parameters/direction" /></xsl:variable>
<!-- some HTML -->
<table>
<!-- table header -->
<xsl:choose>
<!-- 2 when clauses for the direction of navigation -->
<xsl:otherwise>
<xsl:apply-templates select="child::ResultVector[position()
>= 1 and position() < $numToDisplay]" />
</xsl:otherwise>
</xsl:choose>
</table>
</xsl:template>
<xsl:template match="ResultVector">
<!-- fill in the records from the vector -->
</xsl:template>
I've focussed on the xsl:otherwise clause, since it is the first to be
invoked (I don't set the 'direction' parameter the first time.
The problem is that $numToDisplay is out of scope (or...?):
pattern = 'child::SearchCustomerVector[position() >= 1 and position()
< $numToDisplay]'
VariableReference given for variable out of context or without
definition! Name = numToDisplay
at org.apache.xalan.xslt.XSLTEngineImpl.error
(XSLTEngineImpl.java:1630)
I'm able to use $numToDisplay to set another variable on the same level
as the <xsl:apply-templates ... /> and to print it to the HTML output.
Do you have any idea what causes this? Have I misunderstood the scoping
rules for variables?
There are probably other ways of doing what I want -- since I'm new to
XML, XSL, ... I tried this way...
In advance, thanks a lot!!!
Regards,
Frank Olsen