Link to home
Start Free TrialLog in
Avatar of BeenSwank
BeenSwank

asked on

XSLT Variable Scope

Have the following problem, its more of a syntax/grammar issue than a problem. but the idea is that you have a case statement that gives you the node then you do the for each that outputs it. Gives the error: A reference to variable or parameter 'venSearch' cannot be resolved. The variable or parameter may not be defined, or it may not be in scope.

<xsl:if test="...condition1...">
  <xsl:variable name="...name..." select="...node1..."/>
</xsl:if>
<xsl:if test="...condition2...">
  <xsl:variable name="...name..." select="...node2..."/>
</xsl:if>  

<xsl:for-each select="$...name..."/>
...
...
(aka 200 lines of code specific to this condition that I dont want to repeat however many times for each condition)
...
...
</xsl:for-each>
SOLUTION
Avatar of R7AF
R7AF
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you paste the code for the calling page......
It can be referenced as @R7AF suggested...

<xsl:if test="price > 10">
        <xsl:value-of select="title"/>
        <xsl:value-of select="artist"/>
</xsl:if>
You put the variable inside the if-statement. Then the if-statement is the scope, and the variable can only be used inside the if-statement. In your example it's useless.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BeenSwank
BeenSwank

ASKER

Works well jkmyoung, but didnt need to elaborate on output Final script:

<xsl:variable name="venSearch">
      <xsl:if test="$k_location='Warwickshire'">
         <xsl:value-of select="document(concat($docOp, $k_category, '_', $k_type, $docCl))//venue[county='Warwickshire']"/>
      </xsl:if>
  <xsl:if test="$k_location='West Midlands'">
         <xsl:value-of select="document(concat($docOp, $k_category, '_', $k_type, $docCl))//venue[county='West Midlands']"/>
      </xsl:if>
</xsl:variable>

<xsl:value-of select="$venSearch"/>
If only one of the conditions will ever be true, you could use a choose, when clause instead; it'll slightly boost peformance, by stopping at the first clause that is true. Of course, if you're not worried about that you can ignore this post altogether.

<xsl:variable name="venSearch">
  <xsl:choose>
      <xsl:when test="$k_location='Warwickshire'">
         <xsl:value-of select="document(concat($docOp, $k_category, '_', $k_type, $docCl))//venue[county='Warwickshire']"/>
      </xsl:when>
  <xsl:when test="$k_location='West Midlands'">
         <xsl:value-of select="document(concat($docOp, $k_category, '_', $k_type, $docCl))//venue[county='West Midlands']"/>
      </xsl:when>
  </xsl:choose>
</xsl:variable>