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>
BeenSwankAsked:
Who is Participating?
 
jkmyoungConnect With a Mentor Commented:
So do you want one variable which contains a list of nodes? If so, try as presented before, make the variable actually contain multiple nodes:
<xsl:variable name="master">
  <xsl:if test="...condition1...">
    <name><xsl:value-of select="...node1..."/></name>
  </xsl:if>
  <xsl:if test="...condition2...">
    <name><xsl:value-of select="...node2..."/></name>
  </xsl:if>
...
</xsl:variable>

Now when you call the for-each on this variable, you may need to use a node-set function, eg <xsl:for-each select="msxsl:node-set($master)\name" xmlns:msxsl="urn:schemas-microsoft-com:xslt" e>
0
 
R7AFConnect With a Mentor Commented:
Try this:

<xsl:variable name="test">
  <xsl:if>... </xsl:if>
</xsl:variable>
0
 
bugs021997Commented:
Can you paste the code for the calling page......
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
bugs021997Commented:
It can be referenced as @R7AF suggested...

<xsl:if test="price > 10">
        <xsl:value-of select="title"/>
        <xsl:value-of select="artist"/>
</xsl:if>
0
 
R7AFCommented:
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.
0
 
BeenSwankAuthor Commented:
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"/>
0
 
jkmyoungCommented:
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>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.