Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot declare xsl:variable inside xsl:choose block 1

Status
Not open for further replies.

TonyMarston

Programmer
Dec 8, 1999
20
GB
Is there any good reason why the following code does not work?

<xsl:choose>
<xsl:when test=&quot;foo&quot;>
<xsl:variable name=&quot;var&quot; select=&quot;'foo'&quot; />
</xsl/when>
<xsl:eek:therwise>
<xsl:variable name=&quot;var&quot; select=&quot;'something else'&quot; />
</xsl:eek:therwise>
</xsl:choose>

If I try to access variable $var after this code has been executed all I get is &quot;unknown variable&quot;. If I declare the variable without the conditions it works, but I want to set the variable to a different value depending on conditions at run-time.

Is this a bug, or something which is disallowed in the XSL specification?
 
You can start by changing

Code:
  </xsl/when>

to

Code:
  </xsl:when>

Let us know if that changes anything.
 
Whoops. That was a simple typo when constructing the post. The code I actually ran was syntactically correct, but I still get the error.

I have tried the following XSL parsers:
- the Sablotron extension in PHP
- Komodo (XSL debugger) from Activestate
- Microsoft's MSXML parser v4

The same error appears in all three. I have read the XSL specification several times regarding variables, but I cannot see why it should be disallowed. If I declare the variable outside the condition statement it works, but as soon as I put it inside a condition it does not matter which statement gets processed as it becomes invisible outside of the condition block.
 
How about posting the complete XSL file? You've got me guessing about too many things, including the declaration.
 
Here it is in its entirety. It is actrually in a template that is called from a number of stylesheets.

Code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL] version=&quot;1.0&quot;>

<xsl:param name=&quot;curpage&quot;>2</xsl:param>
<xsl:param name=&quot;lastpage&quot;>5</xsl:param>
<xsl:param name=&quot;session_name&quot;></xsl:param>

<!-- create hyperlinks to jump to first/prev/next/last page of current details -->
<xsl:template name=&quot;pagination&quot;>
  
  <xsl:choose>
    <xsl:when test=&quot;$session_name&quot;>
      <xsl:variable name=&quot;session&quot; select=&quot;concat('&session_name=',$session_name)&quot; />
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name=&quot;session&quot; select=&quot;''&quot; />
    </xsl:otherwise>
  </xsl:choose>
   
  <table class=&quot;pagination&quot;>
    <tr class=&quot;pagination&quot;>
    
      <xsl:choose>
        <xsl:when test=&quot;$curpage<=1&quot;>
          <!-- we are on page 1, so there is no navigation backwards -->
          <td class=&quot;pagination&quot;>FIRST</td>
          <td class=&quot;pagination&quot;>PREV</td>
        </xsl:when>
        <xsl:otherwise>
          <!-- insert links for first/previous page -->
          <td class=&quot;pagination&quot;><a href=&quot;{$script}?page=1{$session}&quot;><b>FIRST</b></a></td>
          <td class=&quot;pagination&quot;><a href=&quot;{$script}?page={$curpage -1}{$session}&quot;><b>PREV</b></a></td>
        </xsl:otherwise>
      </xsl:choose>
    
      <!-- insert &quot;page x of y&quot; -->
      <td class=&quot;pagination&quot;>
        <xsl:text>Page </xsl:text><xsl:value-of select=&quot;$curpage&quot;/>
        <xsl:text> of </xsl:text><xsl:value-of select=&quot;$lastpage&quot;/>
      </td>
      
      <xsl:choose>
        <xsl:when test=&quot;$curpage=$lastpage&quot;>
          <!-- we are on the last page, so there is no navigation forwards -->
          <td class=&quot;pagination&quot;>NEXT</td>
          <td class=&quot;pagination&quot;>LAST</td>
        </xsl:when>
        <xsl:otherwise>
          <!-- insert links for last/next page -->
          <td class=&quot;pagination&quot;><a href=&quot;{$script}?page={$curpage +1}{$session}&quot;><b>NEXT</b></a></td>
          <td class=&quot;pagination&quot;><a href=&quot;{$script}?page={$lastpage}{$session}&quot;><b>LAST</b></a></td>
        </xsl:otherwise>
      </xsl:choose>
      
    </tr>
  </table>
  
</xsl:template>

</xsl:stylesheet>
 
I had a similiar problem and found that the variable had to be outside the choose. Here is how mine ended up.

<xsl:variable name=&quot;gender&quot;>
<xsl:choose>
<xsl:when test=&quot;sex='M'&quot;>Male</xsl:when>
<xsl:when test=&quot;sex='F'&quot;>Female</xsl:when>
<xsl:when test=&quot;sex='U'&quot;>Unknown</xsl:when>
</xsl:choose>
</xsl:variable>

Hope this helps
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top