xsl-list
[Top] [All Lists]

RE: Reference to variable cannot be resolved.

2003-02-13 11:37:45
From: Imrran Wahid [mailto:devguy2003(_at_)yahoo(_dot_)com]
Sent: Thursday, February 13, 2003 11:19 AM
Subject: [xsl] Reference to variable cannot be resolved.

I get the following error when attempting to transform
an xml using IE 6.

"A reference to variable or parameter 'ItemAStruct'
cannot be resolved. The variable or parameter may not
be defined, or it may not be in scope."

Here's a snippet from my xsl:
<xsl:template match="/">
  <html>
   <body bgcolor="#B0C4DE">
    <xsl:variable name="ItemAStruct"
select="document('lookup.xml')/TableStructures/ItemA"/>
    
    <xsl:apply-templates select="//Content/ItemAs" /> 
  </body>
  </html>
</xsl:template>

<xsl:template match="ItemAs">
  <xsl:variable name="ItemAs" select="."/>
  <!-- process each ItemA element from lookup.xml -->
  <tr>
  <xsl:for-each select="$ItemAStruct">
    <xsl:variable name="ItemName"
select="string(name(.))"/>
    <xsl:if test="count($ItemAs/*[name(.)=$ItemName])
&gt; 0">
      <th><xsl:value-of select="$ItemName"/></th>
    </xsl:if>
  </xsl:for-each>
  </tr>
</xsl:template>
</xsl:stylesheet>


Can anyone spot the culprit?

It's a scope issue: $ItemAStruct exists only within its local template, so
your matching template can't "see" it.

You have two options: make $ItemAStruct a global variable (i.e., move its
xsl:variable declaration outside your templates), or better yet, pass it as
a parameter:

<xsl:apply-templates select="//Content/ItemAs">
  <xsl:with-param name="ItemAStruct"
select="document('lookup.xml')/TableStructures/ItemA"/>
</xsl:apply-templates>

<xsl:template match="ItemAs">
  <xsl:param name="ItemAStruct"/>
  <!-- rest of code -->
</xsl:template>

hth,
b.

| brian martinez                              
brian(_dot_)martinez(_at_)trip(_dot_)com |
| senior gui programmer                                  303.708.7248 |
| trip network, inc.                                 fax 303.790.9350 |
| 6436 s. racine cir.                             englewood, co 80111 |
| http://www.cheaptickets.com/                   http://www.trip.com/ |

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