Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: Peter Flynn <peter.nosp@-.--------.-->
To: NULL
Date: 9/15/2007 5:38:00 PM

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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent