Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get boolean (true/false) values as boolean variables?

4,839 views
Skip to first unread message

David Lowndes

unread,
Sep 14, 2007, 5:41:04 PM9/14/07
to
Currently I have an XML file with what are really boolean attribute
values being passed numerically as 0 or 1:

<MyElem flag="1"/>

and in the XSLT I get the value like this:

<xsl:param name='MyFlag'
select='number($MyDocument/MyDoc/MyElem/@flag)' />

It's more appropriate if I represent the flag as a boolean:

<MyElem flag="true"/>

.. but I can't find what the equivalent would be to get the boolean
value variable in the XSLT.

Is there a way of doing what I want?

Dave Lowndes

Anthony Jones

unread,
Sep 15, 2007, 8:43:57 AM9/15/07
to
"David Lowndes" <Dav...@example.invalid> wrote in message
news:7gvle3hb6skovc69u...@4ax.com...

> Currently I have an XML file with what are really boolean attribute
> values being passed numerically as 0 or 1:
>
> <MyElem flag="1"/>
>
> and in the XSLT I get the value like this:
>
> <xsl:param name='MyFlag'
> select='number($MyDocument/MyDoc/MyElem/@flag)' />
>
> It's more appropriate if I represent the flag as a boolean:
>
> <MyElem flag="true"/>
>


Why?

I use 0 and -1. These are the values that I find implicit coerce to boolean
the best in majority of languages I use.

> .. but I can't find what the equivalent would be to get the boolean
> value variable in the XSLT.
>
> Is there a way of doing what I want?

An alternative is simply not have the flag element present at all in the XML
if its value is to be treated as false. I find that to be the most XSL
friendly approach.

--
Anthony Jones - MVP ASP/ASP.NET


Martin Honnen

unread,
Sep 15, 2007, 8:45:31 AM9/15/07
to

I am not sure I understand what you are looking for. If the flag
attribute has the value "0" then you can use
boolean(@flag)
to get the boolean value false, for other numbers including 1 you get
the boolean value true.

If you have the attribute value "true" or "false" and want to convert
that to a boolean then use e.g.
<xsl:variable name="flag">
<xsl:choose>
<xsl:when test="@flag = 'true'">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="false()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

With XPath 2.0 you could also simply do e.g.
if (@flag = 'true') then true() else false()
or you can use xs:boolean(@flag) to construct a boolean value.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Peter Flynn

unread,
Sep 15, 2007, 12:38:16 PM9/15/07
to
David Lowndes wrote:
> Currently I have an XML file with what are really boolean attribute
> values being passed numerically as 0 or 1:
>
> <MyElem flag="1"/>
>
> and in the XSLT I get the value like this:
>
> <xsl:param name='MyFlag'
> select='number($MyDocument/MyDoc/MyElem/@flag)' />
>
> It's more appropriate if I represent the flag as a boolean:
>
> <MyElem flag="true"/>

If by "appropriate" you mean "easier for the author or editor of a
manually-written document to understand" then I agree (I am assuming
that your element type is not really called MyElem and that your
attribute is not reall called flag).

When XML is used for authoring (as opposed to being machine-written by
some other automated process), it is A Good Idea to make it as
human-friendly as possible, and offload the burden of interpretation to
the XSLT or other processing environment.

Most programmers, engineers, and designers take the opposite view: that
the XML should be made as program-friendly as possible, and offload the
burden of comprehension onto the human. This is generally A Bad Idea if
the document is to be composed by a human.

It was OK in the bad old days when computers cost millions and program
development was measured in years. But nowadays computers cost only
hundreds, and programs can be written in hours. Humans are the ones who
cost the money these days, and programs (and document types) need to
make things as easy for humans as possible. Unfortunately a lot of
software developers and companies have only paid lip-service to this
requirement (one result is that our current crop of XML editors is
geared towards the XML expert developer, not the document author).

But as Anthony has pointed out, declare the attribute with a default
value (the most commonly-used one) so that the author can simply omit it
in most cases, and only specify it when needed, eg

<!ATTLIST MyElem flag (true|false) "true">

(or "false" as appropriate). According to the semantics of what your
attribute is really called, values of (yes|no) "yes" might be another
way to express this.

> .. but I can't find what the equivalent would be to get the boolean
> value variable in the XSLT.

<xsl:param name="MyFlag">
<xsl:choose>
<xsl:when test="$MyDocument/MyDoc/MyElem/@flag='true'">
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>0</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:param>

> Is there a way of doing what I want?

If this really is a human-writable document type, there is another way:

<!ELEMENT MyElem (whatever)>
<!ATTLIST MyElem flag (1|0) "1">
<!ENTITY true "1">
<!ENTITY false "0">
...
<MyElem flag="&true;">

This is two characters more to type, but has the advantage that the
processor will receive the value "1" or "0" from the parser.

Iff this is *not* a human-writable document type, then the foregoing is
largely irrelevant, because computers neither know nor care what hidden
complexities underly the choice of datatypes.

///Peter

David Lowndes

unread,
Sep 16, 2007, 5:43:05 AM9/16/07
to
>With XPath 2.0 you could also simply do e.g.
> if (@flag = 'true') then true() else false()
>or you can use xs:boolean(@flag) to construct a boolean value.

Ah, that was what I was expecting to be able to do - guess the long
winded version will do for now.

Thanks
Dave

David Lowndes

unread,
Sep 16, 2007, 6:55:48 AM9/16/07
to
Having now tried things:

>I am not sure I understand what you are looking for. If the flag
>attribute has the value "0" then you can use
> boolean(@flag)
>to get the boolean value false, for other numbers including 1 you get
>the boolean value true.

This works:

boolean(number(@flag))

when flag is "0"/"1"

But this:

>If you have the attribute value "true" or "false" and want to convert
>that to a boolean then use e.g.
> <xsl:variable name="flag">
> <xsl:choose>
> <xsl:when test="@flag = 'true'">
> <xsl:value-of select="true()"/>
> </xsl:when>
> <xsl:otherwise>
> <xsl:value-of select="false()"/>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:variable>

results in the variable being a node with a value true/false rather
than just a boolean value.

Perhaps I should mention that the reason I'd preferred to have
"true"/"false" for the attribute is that the XML is generated using
the .Net XMLSerialization which creates "true"/"false" for bool class
members by default.

Dave

Dimitre Novatchev

unread,
Sep 16, 2007, 10:33:27 AM9/16/07
to
> If you have the attribute value "true" or "false" and want to convert that
> to a boolean then use e.g.
> <xsl:variable name="flag">
> <xsl:choose>
> <xsl:when test="@flag = 'true'">
> <xsl:value-of select="true()"/>
> </xsl:when>
> <xsl:otherwise>
> <xsl:value-of select="false()"/>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:variable>
>

This can be written in a shorter form as:

<xsl:variable name="flag" select="@flag = 'true' ">


Cheers,
Dimitre Novatchev


"Martin Honnen" <maho...@yahoo.de> wrote in message
news:eMRQTZ59...@TK2MSFTNGP04.phx.gbl...

David Lowndes

unread,
Sep 16, 2007, 12:50:30 PM9/16/07
to
>> If you have the attribute value "true" or "false" and want to convert that
>> to a boolean then use e.g.
>> <xsl:variable name="flag">
>> <xsl:choose>
>> <xsl:when test="@flag = 'true'">
>> <xsl:value-of select="true()"/>
>> </xsl:when>
>> <xsl:otherwise>
>> <xsl:value-of select="false()"/>
>> </xsl:otherwise>
>> </xsl:choose>
>> </xsl:variable>
>>
>
>This can be written in a shorter form as:
>
> <xsl:variable name="flag" select="@flag = 'true' ">

It's not the same as the long form - it's better as it works just fine
for what I need :)

Thanks
Dave

0 new messages