By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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.