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

Re: effecient inline multi-conditional testing

Subject: Re: effecient inline multi-conditional testing
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 20 Nov 2001 15:35:27 +0000
invalid boolean value
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


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.