Re: [xsl] 'true' or true()

Subject: Re: [xsl] 'true' or true()
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Mon, 01 Nov 2010 21:54:39 +0000
On 01/11/2010 21:14, Karl Stubsjoen wrote:
I have a parameter which will either be, true() by default or 'true'
(set by user).  Given the name isTrue, how do you properly write the
if test below:

<xsl:param name="isTrue" select="true()"/>
<xsl:if test="$isTrue or isTrue=true()">

When user sets the param value = 'false' the above isTrue test still passes.

Thanks,
Karl..

In XSLT 2.0, declare the expected type of the parameter: <xsl:param name="isTrue" select="true()" as="xs:boolean"/>.

The way parameters are passed to a stylesheet depends on the product and API you are using: for example from the Saxon command line you will be able to pass the string values "true", "false", "0", or "1", but the important thing is that whatever is passed, you know you are dealing with a boolean, and not (say) a string.

If you can't do this, then validate the parameter within your code:

<xsl:when test="$isTrue='true'">...
<xsl:when test="$isTrue='false'">...
<xsl:otherwise><xsl:message...

Don't just take the effective boolean value. The EBV of all strings other than "" is true.

Michael Kay
Saxonica

Current Thread