473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 18686
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
In the following loop, at the end, the method debugNotes() is printing out some notes for me to read, so I can figure out what is going on in my script. Where I try to print out the value of the...
10
by: Sylvain Thenault | last post by:
Hi there ! Can someone explain me the following behaviour ? >>> l = >>> 0 in (l is False) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iterable argument...
1
by: nick | last post by:
Hi What is the proper way of setting a boolean column (checkbox) to "true" by default? It seems that the datagrid won't accept inserts if the default is true. BoolColPaid = new...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
10
by: Daniel Crespo | last post by:
Hello to all, How can I do new_variable = (variable) ? True : False; in Python in one line? I want to do something like this:
59
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
4
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, what is the use-case of parameter "start" in string's "endswith" method? Consider the following minimal example: a = "testing" suffix="ing" a.endswith(suffix, 2) Significance of...
25
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class...
2
by: Rafael Douglas | last post by:
Hello All, I was writing a program and ran across this error: error LNK2019: unresolved external symbol "double __cdecl setTotalCost(void)" (?setTotalCost@@YANXZ) referenced in function _main...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.