[xsl] boolean variables

Subject: [xsl] boolean variables
From: "Robin Harvey" <harvey.robin@xxxxxxxxx>
Date: Wed, 9 Aug 2006 16:13:40 +0100
Hi,

I'm having a hard time dealing with boolean values in variables, and
i'm not sure if the approach i'm taking is possible/sensible.  As far
as i understand, the xpath boolean() function should return false for
the empty string, 0, boolean false and the empty node-set; and true
for all other values.  I want to be able to store values in a
variable, and then test the variable in a xsl:if, but it always
evaluates to true.  Here is a small test file, which shows the
different methods i've tried.  None of them work!

<?xml version="1.0"?>
<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   version="1.0">

   <xsl:template match="/">
       <xsl:variable name="true">
           <xsl:call-template name="get-true"/>
       </xsl:variable>

       <xsl:variable name="false">
           <xsl:call-template name="get-false"/>
       </xsl:variable>

true: <xsl:value-of select="$true"/>

false: <xsl:value-of select="$false"/>

       Test1:
       <xsl:call-template name="boolean-check">
           <xsl:with-param name="var" select="$true"/>
       </xsl:call-template>

       <xsl:call-template name="boolean-check">
           <xsl:with-param name="var" select="$false"/>
       </xsl:call-template>

       Test2:
       <xsl:call-template name="boolean-check2">
           <xsl:with-param name="var" select="$true"/>
       </xsl:call-template>

       <xsl:call-template name="boolean-check2">
           <xsl:with-param name="var" select="$false"/>
       </xsl:call-template>

       Test3:
       <xsl:call-template name="boolean-check3">
           <xsl:with-param name="var" select="$true"/>
       </xsl:call-template>

       <xsl:call-template name="boolean-check3">
           <xsl:with-param name="var" select="$false"/>
       </xsl:call-template>


</xsl:template>




   <xsl:template name="boolean-check">
       <xsl:param name="var"/>

       <xsl:choose>
           <xsl:when test="$var">
               Variable is true
           </xsl:when>
           <xsl:otherwise>
               Variable is false
           </xsl:otherwise>
       </xsl:choose>
   </xsl:template>

   <xsl:template name="boolean-check2">
       <xsl:param name="var"/>

       <xsl:choose>
           <xsl:when test="boolean($var)">
               Variable is true
           </xsl:when>
           <xsl:otherwise>
               Variable is false
           </xsl:otherwise>
       </xsl:choose>

</xsl:template>

   <xsl:template name="boolean-check3">
       <xsl:param name="var"/>

       <xsl:choose>
           <xsl:when test="$var = true()">
               Variable is true
           </xsl:when>
           <xsl:otherwise>
               Variable is false
           </xsl:otherwise>
       </xsl:choose>

</xsl:template>


<xsl:template name="get-true"> <xsl:value-of select="true()"/> </xsl:template>

   <xsl:template name="get-false">
       <xsl:value-of select="false()"/>
   </xsl:template>

</xsl:stylesheet>


I'm getting the feeling that i've seriously missed the point somewhere along the line, could anyone explain why this code fails, and the proper way to deal with booleans in variables?

Many thanks

--Robin

Current Thread