[Home] [By Thread] [By Date] [Recent Entries]
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-false">
<xsl:value-of select="false()"/>
</xsl:template></xsl:stylesheet>
Many thanks --Robin
|

Cart



