You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by "Thomas F. O'Connell" <tf...@netcentral.com> on 2003/06/20 23:54:31 UTC

boolean variable evaluation

I've got a question about Xalan's behavior regarding setting booleans to
explicit functions.

If I've got a stylesheet that includes the following:

<xsl:variable name="boolean_test">
	<xsl:value-of select="false()" />
</xsl:variable>

And I then use it in this sort of expression:

<xsl:if test="boolean_test">
	<message>This is not what I was expecting</message>
</xsl:if>

I'll get the message every time. Is that correct and expected behavior,
or is it a bug?

-tfo


RE: boolean variable evaluation

Posted by "Thomas F. O'Connell" <tf...@netcentral.com>.
Ah, that got it. Wasn't thinking about the distinction. It did indeed
help! Thanks!

-tfo

On Fri, 2003-06-20 at 17:16, Gary L Peskin wrote:
> Thomas --
> 
> You are not creating a boolean variable but a result tree fragment which is
> never empty.
> 
> Try:
> 
>   <xsl:variable name="boolean_test" select="false()"/>
> 
> and see what happens.
> 
> HTH,
> Gary


RE: boolean variable evaluation

Posted by Gary L Peskin <ga...@firstech.com>.
Thomas --

You are not creating a boolean variable but a result tree fragment which is
never empty.

Try:

  <xsl:variable name="boolean_test" select="false()"/>

and see what happens.

HTH,
Gary

> -----Original Message-----
> From: Thomas F. O'Connell [mailto:tfo@netcentral.com] 
> Sent: Friday, June 20, 2003 3:00 PM
> To: xalan-c-users@xml.apache.org
> Cc: xalan-dev@xml.apache.org
> Subject: Re: boolean variable evaluation
> 
> 
> Sorry, I've not been getting enough sleep. Restating my first 
> sentence:
> 
> I've got a question about Xalan's behavior regarding setting 
> variables explicitly to the results of boolean functions.
> 
> Restating my example use of the variable:
> 
> <xsl:if test="$boolean_test">
> 	<message>This is not what I was expecting</message>
> </xsl:if>
> 
> -tfo
> 
> On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> > I've got a question about Xalan's behavior regarding 
> setting booleans 
> > to explicit functions.
> > 
> > If I've got a stylesheet that includes the following:
> > 
> > <xsl:variable name="boolean_test">
> > 	<xsl:value-of select="false()" />
> > </xsl:variable>
> > 
> > And I then use it in this sort of expression:
> > 
> > <xsl:if test="boolean_test">
> > 	<message>This is not what I was expecting</message> </xsl:if>
> > 
> > I'll get the message every time. Is that correct and expected 
> > behavior, or is it a bug?
> > 
> > -tfo
> > 
> 


Re: boolean variable evaluation

Posted by da...@us.ibm.com.



Hi Thomas,

> > <xsl:variable name="boolean_test">
> >          <xsl:value-of select="false()" />
> > </xsl:variable>

This does not create a boolean variable.  It creates a result tree fragment
containing a single text node with the value "false".  RTFs have a unique
property that their boolean value is always true, even when empty.  (Not
that this one is empty...)

You probably meant to do this:

   <xsl:variable name="boolean_test" select="false()"/>

Dave



|---------+--------------------------->
|         |           "Thomas F.      |
|         |           O'Connell"      |
|         |           <tfo@netcentral.|
|         |           com>            |
|         |                           |
|         |           06/20/2003 02:59|
|         |           PM              |
|---------+--------------------------->
  >---------------------------------------------------------------------------------------------------------------|
  |                                                                                                               |
  |        To:      xalan-c-users@xml.apache.org                                                                  |
  |        cc:      xalan-dev@xml.apache.org, (bcc: David N Bertoni/Cambridge/IBM)                                |
  |        Subject: Re: boolean variable evaluation                                                               |
  >---------------------------------------------------------------------------------------------------------------|



Sorry, I've not been getting enough sleep. Restating my first sentence:

I've got a question about Xalan's behavior regarding setting variables
explicitly to the results of boolean functions.

Restating my example use of the variable:

<xsl:if test="$boolean_test">
             <message>This is not what I was expecting</message>
</xsl:if>

-tfo

On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> I've got a question about Xalan's behavior regarding setting booleans to
> explicit functions.
>
> If I've got a stylesheet that includes the following:
>
> <xsl:variable name="boolean_test">
>            <xsl:value-of select="false()" />
> </xsl:variable>
>
> And I then use it in this sort of expression:
>
> <xsl:if test="boolean_test">
>            <message>This is not what I was expecting</message>
> </xsl:if>
>
> I'll get the message every time. Is that correct and expected behavior,
> or is it a bug?
>
> -tfo
>




RE: boolean variable evaluation

Posted by Gary L Peskin <ga...@firstech.com>.
Thomas --

You are not creating a boolean variable but a result tree fragment which is
never empty.

Try:

  <xsl:variable name="boolean_test" select="false()"/>

and see what happens.

HTH,
Gary

> -----Original Message-----
> From: Thomas F. O'Connell [mailto:tfo@netcentral.com] 
> Sent: Friday, June 20, 2003 3:00 PM
> To: xalan-c-users@xml.apache.org
> Cc: xalan-dev@xml.apache.org
> Subject: Re: boolean variable evaluation
> 
> 
> Sorry, I've not been getting enough sleep. Restating my first 
> sentence:
> 
> I've got a question about Xalan's behavior regarding setting 
> variables explicitly to the results of boolean functions.
> 
> Restating my example use of the variable:
> 
> <xsl:if test="$boolean_test">
> 	<message>This is not what I was expecting</message>
> </xsl:if>
> 
> -tfo
> 
> On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> > I've got a question about Xalan's behavior regarding 
> setting booleans 
> > to explicit functions.
> > 
> > If I've got a stylesheet that includes the following:
> > 
> > <xsl:variable name="boolean_test">
> > 	<xsl:value-of select="false()" />
> > </xsl:variable>
> > 
> > And I then use it in this sort of expression:
> > 
> > <xsl:if test="boolean_test">
> > 	<message>This is not what I was expecting</message> </xsl:if>
> > 
> > I'll get the message every time. Is that correct and expected 
> > behavior, or is it a bug?
> > 
> > -tfo
> > 
> 


Re: boolean variable evaluation

Posted by "Thomas F. O'Connell" <tf...@netcentral.com>.
Sorry, I've not been getting enough sleep. Restating my first sentence:

I've got a question about Xalan's behavior regarding setting variables
explicitly to the results of boolean functions.

Restating my example use of the variable:

<xsl:if test="$boolean_test">
	<message>This is not what I was expecting</message>
</xsl:if>

-tfo

On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> I've got a question about Xalan's behavior regarding setting booleans to
> explicit functions.
> 
> If I've got a stylesheet that includes the following:
> 
> <xsl:variable name="boolean_test">
> 	<xsl:value-of select="false()" />
> </xsl:variable>
> 
> And I then use it in this sort of expression:
> 
> <xsl:if test="boolean_test">
> 	<message>This is not what I was expecting</message>
> </xsl:if>
> 
> I'll get the message every time. Is that correct and expected behavior,
> or is it a bug?
> 
> -tfo
> 


Re: boolean variable evaluation

Posted by da...@us.ibm.com.



Hi Thomas,

Yes, generic XSLT questions are best asked on the Mulberry list. Not only
will you get a faster response there, but you will likely get multiple
responses, which may help you figure out the best way to do something.

In XSLT 1.0, any xsl:variable element that contains content creates a
result tree fragment, and, in XPath 1.0, you cannot use if/else logic in
expressions.  On the other hand, there are a surprising number of tricks to
get XPath 1.0 to do what you want, so you should definitely post your
questions to the Mulberry list.

As a last result, you can always create a result tree fragment, and compare
the result as a string:

   <xsl:variable name="boolean_test">
             <xsl:choose>
                         <!-- imagine a more rigorous test, here... -->
                         <xsl:when test="true()"><xsl:value-of
   select="true()"/></xsl:when>
                         <xsl:otherwise><xsl:value-of
   select="false()"/></xsl:otherwise>
             </xsl:choose>
   </xsl:variable>

   <xsl:if test="$boolean_test = string(true())">
   ...

Dave



|---------+--------------------------->
|         |           "Thomas F.      |
|         |           O'Connell"      |
|         |           <tfo@netcentral.|
|         |           com>            |
|         |                           |
|         |           06/23/2003 10:07|
|         |           AM              |
|---------+--------------------------->
  >---------------------------------------------------------------------------------------------------------------|
  |                                                                                                               |
  |        To:      xalan-c-users@xml.apache.org                                                                  |
  |        cc:      (bcc: David N Bertoni/Cambridge/IBM)                                                          |
  |        Subject: Re: boolean variable evaluation                                                               |
  >---------------------------------------------------------------------------------------------------------------|



Thanks to all who pointed out the subtleties of the difference between
getting back a result tree fragment rather than true boolean output.

So here's my next question:

Is there a way to achieve the same effect (i.e., not getting a result
tree fragment) if you want to set a variable conditionally?

For example, suppose I wanted to do something like this:

<xsl:variable name="boolean_test">
             <xsl:choose>
                         <!-- imagine a more rigorous test, here... -->
                         <xsl:when test="true()">
                                     <!-- somehow set boolean test to
true() -->
                         </xsl:when>
                         <xsl:otherwise>
                                     <!-- somehow set boolean_test to
false() -->
                         </xsl:otherwise>
             </xsl:choose>
</xsl:variable>

Currently, I'm setting the value of the variable to the test itself, but
I can foresee cases where one might want to include a more complicated
XSL structure in the body of a variable tag such that directly using a
select isn't sufficient. In such a case, will the result always be a
result tree fragment?

If this is better posted to an XSL(T) list, I can redirect it. But it's
following up on an existing thread, so I figured I'd give it a shot.

Thanks!

-tfo

On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> I've got a question about Xalan's behavior regarding setting booleans to
> explicit functions.
>
> If I've got a stylesheet that includes the following:
>
> <xsl:variable name="boolean_test">
>            <xsl:value-of select="false()" />
> </xsl:variable>
>
> And I then use it in this sort of expression:
>
> <xsl:if test="boolean_test">
>            <message>This is not what I was expecting</message>
> </xsl:if>
>
> I'll get the message every time. Is that correct and expected behavior,
> or is it a bug?
>
> -tfo




Re: boolean variable evaluation

Posted by "Thomas F. O'Connell" <tf...@netcentral.com>.
Thanks to all who pointed out the subtleties of the difference between
getting back a result tree fragment rather than true boolean output.

So here's my next question:

Is there a way to achieve the same effect (i.e., not getting a result
tree fragment) if you want to set a variable conditionally?

For example, suppose I wanted to do something like this:

<xsl:variable name="boolean_test">
	<xsl:choose>
		<!-- imagine a more rigorous test, here... -->
		<xsl:when test="true()">
			<!-- somehow set boolean test to true() -->
		</xsl:when>
		<xsl:otherwise>
			<!-- somehow set boolean_test to false() -->
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

Currently, I'm setting the value of the variable to the test itself, but
I can foresee cases where one might want to include a more complicated
XSL structure in the body of a variable tag such that directly using a
select isn't sufficient. In such a case, will the result always be a
result tree fragment?

If this is better posted to an XSL(T) list, I can redirect it. But it's
following up on an existing thread, so I figured I'd give it a shot.

Thanks!

-tfo

On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> I've got a question about Xalan's behavior regarding setting booleans to
> explicit functions.
> 
> If I've got a stylesheet that includes the following:
> 
> <xsl:variable name="boolean_test">
> 	<xsl:value-of select="false()" />
> </xsl:variable>
> 
> And I then use it in this sort of expression:
> 
> <xsl:if test="boolean_test">
> 	<message>This is not what I was expecting</message>
> </xsl:if>
> 
> I'll get the message every time. Is that correct and expected behavior,
> or is it a bug?
> 
> -tfo


Re: boolean variable evaluation

Posted by "Thomas F. O'Connell" <tf...@netcentral.com>.
Sorry, I've not been getting enough sleep. Restating my first sentence:

I've got a question about Xalan's behavior regarding setting variables
explicitly to the results of boolean functions.

Restating my example use of the variable:

<xsl:if test="$boolean_test">
	<message>This is not what I was expecting</message>
</xsl:if>

-tfo

On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> I've got a question about Xalan's behavior regarding setting booleans to
> explicit functions.
> 
> If I've got a stylesheet that includes the following:
> 
> <xsl:variable name="boolean_test">
> 	<xsl:value-of select="false()" />
> </xsl:variable>
> 
> And I then use it in this sort of expression:
> 
> <xsl:if test="boolean_test">
> 	<message>This is not what I was expecting</message>
> </xsl:if>
> 
> I'll get the message every time. Is that correct and expected behavior,
> or is it a bug?
> 
> -tfo
> 


Re: boolean variable evaluation

Posted by "Thomas F. O'Connell" <tf...@netcentral.com>.
Sorry, I've not been getting enough sleep. Restating my first sentence:

I've got a question about Xalan's behavior regarding setting variables
explicitly to the results of boolean functions.

Restating my example use of the variable:

<xsl:if test="$boolean_test">
	<message>This is not what I was expecting</message>
</xsl:if>

-tfo

On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> I've got a question about Xalan's behavior regarding setting booleans to
> explicit functions.
> 
> If I've got a stylesheet that includes the following:
> 
> <xsl:variable name="boolean_test">
> 	<xsl:value-of select="false()" />
> </xsl:variable>
> 
> And I then use it in this sort of expression:
> 
> <xsl:if test="boolean_test">
> 	<message>This is not what I was expecting</message>
> </xsl:if>
> 
> I'll get the message every time. Is that correct and expected behavior,
> or is it a bug?
> 
> -tfo
> 


Re: boolean variable evaluation

Posted by "Thomas F. O'Connell" <tf...@netcentral.com>.
Sorry, I've not been getting enough sleep. Restating my first sentence:

I've got a question about Xalan's behavior regarding setting variables
explicitly to the results of boolean functions.

Restating my example use of the variable:

<xsl:if test="$boolean_test">
	<message>This is not what I was expecting</message>
</xsl:if>

-tfo

On Fri, 2003-06-20 at 16:54, Thomas F. O'Connell wrote:
> I've got a question about Xalan's behavior regarding setting booleans to
> explicit functions.
> 
> If I've got a stylesheet that includes the following:
> 
> <xsl:variable name="boolean_test">
> 	<xsl:value-of select="false()" />
> </xsl:variable>
> 
> And I then use it in this sort of expression:
> 
> <xsl:if test="boolean_test">
> 	<message>This is not what I was expecting</message>
> </xsl:if>
> 
> I'll get the message every time. Is that correct and expected behavior,
> or is it a bug?
> 
> -tfo
>