Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - How to get boolean (true/false) values as boolean variables? [Thread Next] Re: How to get boolean (true/false) values as boolean variables?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 | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
