This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: effecient inline multi-conditional testing
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: Jeff Beadle <Jbeadle at manh dot com>
- Cc: xsl-list at lists dot mulberrytech dot com
- Date: Tue, 20 Nov 2001 15:35:27 +0000
- Subject: Re: [xsl] effecient inline multi-conditional testing
- Organization: Jeni Tennison Consulting Ltd
- References: <60A8664AEEB2D511B631000476AAB1A35A657D@ma-atl81>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Jeff,
> here's the task:
> -create a template to cast an overloaded boolean value
I'd probably use separate variables for boolean true and boolean
false, perhaps based on the $cast parameter, and then return the
appropriate one of those, so something like:
<xsl:template name="cast:boolean">
<xsl:param name="b-value" />
<xsl:param name="cast" select="'true-false'" />
<xsl:variable name="true" select="substring-before($cast, '-')" />
<xsl:variable name="false" select="substring-after($cast, '-')" />
<xsl:choose>
<xsl:when test="$b-value = 'true' or $b-value = 't' or
$b-value = 'yes' or $b-value = 'y' or
$b-value = 1">
<xsl:value-of select="$true" />
</xsl:when>
<xsl:when test="$b-value = 'false' or $b-value = 'f' or
$b-value = 'no' or $b-value = 'n' or
$b-value = 0">
<xsl:value-of select="$false" />
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
ERROR: Invalid boolean value '<xsl:value-of select="$b-value" />'
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Another option is to store all the possible 'true' and 'false' values
in an XML structure that you can then test against. For example:
<values type="boolean-true">
<value>true</value>
<value>t</value>
<value>yes</value>
<value>y</value>
<value>1</value>
</values>
<values type="boolean-false">
<value>false</value>
<value>f</value>
<value>no</value>
<value>n</value>
<value>0</value>
</values>
You could then store the relevant value elements in $boolean-trues and
$boolean-falses variables using your preferred method (document('') or
node-set() function, or use XSLT 1.1), and test against them:
<xsl:template name="cast:boolean">
<xsl:param name="b-value" />
<xsl:param name="cast" select="'true-false'" />
<xsl:variable name="true" select="substring-before($cast, '-')" />
<xsl:variable name="false" select="substring-after($cast, '-')" />
<xsl:choose>
<xsl:when test="$b-value = $boolean-trues">
<xsl:value-of select="$true" />
</xsl:when>
<xsl:when test="$b-value = $boolean-falses">
<xsl:value-of select="$false" />
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
ERROR: Invalid boolean value '<xsl:value-of select="$b-value" />'
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Or something similar.
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list