469,349 Members | 1,624 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

Parse/convert "True" or "False" to boolean?

I have what should be a trivial problem. I am using XMLSerializer to
serialize an object. It serializes boolean values as "True" and
"False". I then want to use an XSLT on this XML, and I want to use
one of these booleans in a test. How do I convert one of these to a
boolean in a test?!? I know that I could compare it as a string to
"True" or "False", but that seems extremely crude to me. The
"boolean()" function (which, to me, seems like a misnomer) will always
return "true" since the string is not empty. I'm finding it hard to
believe that there isn't a simple way to turn "0", "false", "False",
"no", "No", "1", "-1", "true", "True", "yes", "Yes", etc. into a
boolean! Have I overlooked something obvious, or is this as difficult
as it appears?

Thanks!
Brad.
Nov 12 '05 #1
4 18322
"Bradley Plett" <pl****@hotmail.com> wrote in message news:bo********************************@4ax.com...
How do I convert one of these to a
boolean in a test?!? I know that I could compare it as a string to
"True" or "False", but that seems extremely crude to me.
Looks like a clear solution to me.
I'm finding it hard to
believe that there isn't a simple way to turn "0", "false", "False",
"no", "No", "1", "-1", "true", "True", "yes", "Yes", etc. into a
boolean!
Don't forget "verdad", "falso", "ja", "nein", et al.
"boolean()" function (which, to me, seems like a misnomer) will always
return "true" since the string is not empty.
It's either that definition of falseness, or every XPath processor
will need a world dictionary.

If you must use boolean(), then try removing the characters in the word
"False" to make it an empty string.

boolean(translate(string(@attrName),"fFalse",""))
Have I overlooked something obvious, or is this as difficult
as it appears?


Not to sound like the Oracle from the movie, "The Matrix," but I think
what you're overlooking is.. you already know the answer. ;-)
Derek Harmon
Nov 12 '05 #2
"Bradley Plett" <pl****@hotmail.com> wrote in message news:bo********************************@4ax.com...
How do I convert one of these to a
boolean in a test?!? I know that I could compare it as a string to
"True" or "False", but that seems extremely crude to me.
Looks like a clear solution to me.
I'm finding it hard to
believe that there isn't a simple way to turn "0", "false", "False",
"no", "No", "1", "-1", "true", "True", "yes", "Yes", etc. into a
boolean!
Don't forget "verdad", "falso", "ja", "nein", et al.
"boolean()" function (which, to me, seems like a misnomer) will always
return "true" since the string is not empty.
It's either that definition of falseness, or every XPath processor
will need a world dictionary.

If you must use boolean(), then try removing the characters in the word
"False" to make it an empty string.

boolean(translate(string(@attrName),"fFalse",""))
Have I overlooked something obvious, or is this as difficult
as it appears?


Not to sound like the Oracle from the movie, "The Matrix," but I think
what you're overlooking is.. you already know the answer. ;-)
Derek Harmon
Nov 12 '05 #3
Bradley Plett wrote:
I have what should be a trivial problem. I am using XMLSerializer to
serialize an object. It serializes boolean values as "True" and
"False".
Well, you are wrong. It serializes them as "true" and "false" as per XML
Schema xs:boolean type. xs:boolean defines "true", "1" and "false", "0"
as the only valid values.

I then want to use an XSLT on this XML, and I want to use one of these booleans in a test. How do I convert one of these to a
boolean in a test?!? I know that I could compare it as a string to
"True" or "False", but that seems extremely crude to me. The
"boolean()" function (which, to me, seems like a misnomer) will always
return "true" since the string is not empty. I'm finding it hard to
believe that there isn't a simple way to turn "0", "false", "False",
"no", "No", "1", "-1", "true", "True", "yes", "Yes", etc. into a
boolean! Have I overlooked something obvious, or is this as difficult
as it appears?


In XPath 1.0 (which was released when XML Schema was in early drafts)
one has to check both "1" and "true":

<xsl:if test="foo = 'true' or foo = 1">

PS. It's fixed in XPath 2.0.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4
Bradley Plett wrote:
I have what should be a trivial problem. I am using XMLSerializer to
serialize an object. It serializes boolean values as "True" and
"False".
Well, you are wrong. It serializes them as "true" and "false" as per XML
Schema xs:boolean type. xs:boolean defines "true", "1" and "false", "0"
as the only valid values.

I then want to use an XSLT on this XML, and I want to use one of these booleans in a test. How do I convert one of these to a
boolean in a test?!? I know that I could compare it as a string to
"True" or "False", but that seems extremely crude to me. The
"boolean()" function (which, to me, seems like a misnomer) will always
return "true" since the string is not empty. I'm finding it hard to
believe that there isn't a simple way to turn "0", "false", "False",
"no", "No", "1", "-1", "true", "True", "yes", "Yes", etc. into a
boolean! Have I overlooked something obvious, or is this as difficult
as it appears?


In XPath 1.0 (which was released when XML Schema was in early drafts)
one has to check both "1" and "true":

<xsl:if test="foo = 'true' or foo = 1">

PS. It's fixed in XPath 2.0.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Sylvain Thenault | last post: by
59 posts views Thread by Pierre Quentel | last post: by
4 posts views Thread by =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post: by
25 posts views Thread by Koliber (js) | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.