[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

boolean variables

Subject: boolean variables
From: "Robin Harvey" <harvey.robin@xxxxxxxxx>
Date: Wed, 9 Aug 2006 16:13:40 +0100
boolean variables
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.