Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should booleans in an XML file format be written as true/false or 1/0?

After thirty years of developing binary file formats I'm (finally) writing out an XML document file format.

I've arrived at a part of my document header where I need to write around fifty separate boolean data members, and I was wondering: should I write these as 1/0 or true/false?

I understand the XML spec may go either way. On one hand, 1/0 SEEMS to be less work when reading the file format back in. But since since the file format is small, and cycles are cheap, is it considered proper XML to make the file format as symbolic as possible? If so, is it proper XML to do the same with ALL enum variables (output their symbolic names vs their values -- which could potentially change?)

And on a side note -- I'm generating XML in this form:

<FieldName>true</FieldName>

NOT this form:

<FieldName value="true"/>

After reading DOZENS of conflicting papers and postings (some on Stack Overflow), it SEEMS that the attribute style (example #2) has been mostly frowned upon.

like image 387
SMGreenfield Avatar asked Jul 09 '13 22:07

SMGreenfield


People also ask

How do you represent a boolean in XML?

Syntax of XML boolean xml version="1.0" encoding="utf-8"?> Following is a boolean declaration in schemas. The value of a boolean is an Integer value whose values are '0' and '1', representing logical yes or no. Capital Letters are avoided.

Can XML attributes be empty?

An element with no content is said to be empty. The two forms produce identical results in XML software (Readers, Parsers, Browsers). Empty elements can have attributes.


2 Answers

You're asking essentially what the cultural conventions are in the XML community. The cultural convention is to do your own thing. However, it's worth observing that XSLT and XQuery will serialize a value of type xs:boolean as "true" or "false".

As for elements vs. attributes, it's an old chestnut, and for simple data like this, both forms are perfectly acceptable. Just try to be consistent within a document or family of documents.

like image 146
Michael Kay Avatar answered Sep 28 '22 08:09

Michael Kay


XML is so verbose anyway compared to binary formats that it probably doesn't really matter. But for ease of use and clarity, I would write it as true and false.

like image 22
Jashaszun Avatar answered Sep 28 '22 08:09

Jashaszun