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>
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works well jkmyoung, but didnt need to elaborate on output Final script:
<xsl:variable name="venSearch">
<xsl:if test="$k_location='Warwick shire'">
<xsl:value-of select="document(concat($d ocOp, $k_category, '_', $k_type, $docCl))//venue[county='Wa rwickshire ']"/>
</xsl:if>
<xsl:if test="$k_location='West Midlands'">
<xsl:value-of select="document(concat($d ocOp, $k_category, '_', $k_type, $docCl))//venue[county='We st Midlands']"/>
</xsl:if>
</xsl:variable>
<xsl:value-of select="$venSearch"/>
<xsl:variable name="venSearch">
<xsl:if test="$k_location='Warwick
<xsl:value-of select="document(concat($d
</xsl:if>
<xsl:if test="$k_location='West Midlands'">
<xsl:value-of select="document(concat($d
</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='Warwick shire'">
<xsl:value-of select="document(concat($d ocOp, $k_category, '_', $k_type, $docCl))//venue[county='Wa rwickshire ']"/>
</xsl:when>
<xsl:when test="$k_location='West Midlands'">
<xsl:value-of select="document(concat($d ocOp, $k_category, '_', $k_type, $docCl))//venue[county='We st Midlands']"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="venSearch">
<xsl:choose>
<xsl:when test="$k_location='Warwick
<xsl:value-of select="document(concat($d
</xsl:when>
<xsl:when test="$k_location='West Midlands'">
<xsl:value-of select="document(concat($d
</xsl:when>
</xsl:choose>
</xsl:variable>