RE: [xsl] effecient inline multi-conditional testing

Subject: RE: [xsl] effecient inline multi-conditional testing
From: Jeff Beadle <Jbeadle@xxxxxxxx>
Date: Wed, 21 Nov 2001 14:00:31 -0500
Mike,

Sorry about my email not being to clear, but your correct--something along
those lines.  

My template can be that generic (a t-f dash seperated string), I ended up
providing the ability to override the existing casting values.  However, do
to the fact that the template was to be deployed within our xslt framework I
needed a to provide (a reasonable) set of standard/supported casting boolean
values ... otherwise we'd end up with a multitude of 'casts' and no
conventions.  

I perhaps should have been a little more clear in regards to my question, I
didn't need any help on how to implement my boolean 'casting' template--I
was curious if there was a better, more effecient, way to test multiple
conditions ... inline.  And perhaps that verbiage isn't to clear either,
here's an example of what I mean by "inline multi-conditional testing":
	
if(("one"==some_param)or("two"==some_param)or("five"==some_param)or(...)...)
{...}

I've been meaning to respond to the list that I received a solution and
maybe illustrate what I ended up implementing, but I've been abolutely
swamped and I wasn't sure if anyone would be interested.

I basically went with some excellent guidance by Jeni Tennison (which ended
up being corroborated by quite a few others, noteably:  David Carlisle and
Wendell Piez).  

Something Jeni illuminated me on, on that I passed on to the rest of my
group, was something very simple yet is extremely powerful (at least I
think):  one can inline compare a literal to a node-set of literals (a
node-set with non-empty text-nodes) and xslt parser will compare the simple
literal to each literal (text-node) within the node-set.  Jeni also
suggested that I could keep a predefined (possibly external) of supported
boolean values.  So now essentially I can do the following:

<xsl:stylesheet ...>

  <!-- ... -->

  <xsl:param name="frmxsl:booleans">
    <!-- Note:  all need upper case -->
    <true>1</true>
    <true>TRUE</true>
    <true>YES</true>
    <false>0</false>
    <false>FALSE</false>
    <false>NO</false>
  </xsl:param>

  <!-- ... -->

   <xsl:template name="frmxsl:Cast.Boolean">
	<!--Note:  all word-like booleans returned will, for now, be proper
case.-->
	<xsl:param name="source">
		<!-- overloaded boolean value;  
			 currently handled values are:
				1.  'true' or 'false'
				2.  'yes' or 'no'
				3.  '1' or '0'
		-->				
	</xsl:param>
	<xsl:param name="cast">
		<!-- 'casting' operator;  
			 currently handled values are:
				1.  'true-false'
				2.  'yes-no'
				3.  '1-0'
			 this param may be overriden with the following
grammer:
				$cast = '(your true cast,your false cast)'
				for example,
					...
					<xsl:with-param
name="cast">(hello,good bye)</xsl:with-param>
					...
					if your source was true, then the
output would be :  hello
					if your source was false, then the
output would be:  good bye
		-->
	</xsl:param>
	<xsl:if test="$source">
		<xsl:variable name="s"
select="frmxsls:toUpperCase(string($source))"/>
		<xsl:if test="$s=msxsl:node-set($frmxsl:booleans)/*"><!--
here's the magic -->
			<xsl:variable name="truth"
select="msxsl:node-set($frmxsl:booleans)//true"/><!-- again some more magic
-->
			<xsl:choose>
				<xsl:when test="$cast=''">
					<xsl:choose>
						<xsl:when
test="($s=$truth)">1</xsl:when>
	
<xsl:otherwise>0</xsl:otherwise>			
					</xsl:choose>

				</xsl:when>		
				<xsl:when test="$cast='true-false'">
					<xsl:choose>
						<xsl:when
test="($s=$truth)">True</xsl:when>
	
<xsl:otherwise>False</xsl:otherwise>			
					</xsl:choose>
				</xsl:when>
				<!-- ... -->
				<xsl:otherwise>
					<!-- handle the $cast override -->
   				      <!-- ... -->
				</xsl:otherwise>			
			</xsl:choose>
		</xsl:if>
	</xsl:if>
   </xsl:template>

  <!-- ... -->

</xsl:stylesheet>

Thanks for your help Mike and, once again, thanks again to everyone that
helped me.  Y'all are very gracious with your time and expetise.

-Jeff




-----Original Message-----
From: Michael Kay [mailto:michael.h.kay@xxxxxxxxxxxx]
Sent: Wednesday, November 21, 2001 5:43 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] effecient inline multi-conditional testing


Have I misunderstood, because to me it looks as if you're trying to say

<xsl:choose>
<xsl:when test="substring-before($cast, '-')=$bvalue">true</xsl:when>
<xsl:when test="substring-after($cast, '-')=$bvalue">false</xsl:when>
<xsl:otherwise>error</xsl:otherwise>
</xsl:choose>

Mike Kay


> 
> 
> here's the task:
>   -create a template to cast an overloaded boolean value
> 
> here's the psuedo-template:
> 	<xsl:template name="cast:boolean">
> 		<xsl:param name="b-value">
> 			<!-- overloaded boolean value;  
> 				some potential values may be:
> 					1.  'true' or 'false' 
> or 't' or 'f'
> 					2.  'yes' or 'no' or 'y' or 'n'
> 					3.  '1' or '0'
> 					4.  ...
> 			  -->				
> 		</xsl:param>
> 		<xsl:param name="cast">
> 			<!-- 'casting' operator;  
> 				some potential values may be:
> 					1.  'true-false' or 't-f'
> 					2.  'yes-no' or 'y-n'
> 					3.  '1-0'
> 					4.  ...
> 			  -->
> 		</xsl:param>
> 		<xsl:if test="$operand">
> 			<xsl:if
> test="(($b-value='1')or($b-value='0')or($b-value='true')or($b-
> value='false')
> or($b-value='yes')or($b-value='no') ... )">
> 				<xsl:choose>
> 					<xsl:when
> test="('true-false'=$cast)">
> 						<!-- ... -->
> 
> 					</xsl:when>
> 					<xsl:when 
> test="('yes-no'=$cast)">
> 						<!-- ... -->
> 
> 					</xsl:when>
> 					<xsl:otherwise>
> 						<!-- ... -->
> 					</xsl:otherwise>
> 
> 				</xsl:choose>
> 			</xsl:if>
> 		</xsl:if>
> 	</xsl:template>
> 
> my question is this:
> Any ideas on a more effecient way to conduct the second 
> (xsl:if) test than
> the way I'm about to?
> 
> Maybe some stylesheet scoped param that acts like a mask and 
> then use the
> translate(...) function?  Or, ...?
> 
> If there is a FAQ on this, then I apologize ... I looked and 
> couldn't find
> one.
> 
> Thanks,
> Jeff
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread