XML Schema Boolean Data Type Definition and Values
The XML Schema (xs:boolean) data type represents a
binary logical value that evaluates to either true or false. In XML
Schema Definition (XSD), the standard specifies a two-value domain
mapped to a lexical space containing four valid representations:
true, false, 1, and
0. This article explains the formal schema definition of
the boolean data type, distinguishes between its value space and lexical
space, and examines why the World Wide Web Consortium (W3C) decided to
support both textual and numeric formats.
The Formal XML Schema Definition
The xs:boolean data type is a built-in primitive
datatype defined in the W3C XML Schema Part 2: Datatypes specification.
It is defined using two distinct concepts:
- Value Space: The abstract set of values, consisting
strictly of two states:
{true, false}. - Lexical Space: The set of valid string
representations within an XML document that map to the value space,
defined as:
{true, false, 1, 0}.
When an XML processor validates an element or attribute typed as
xs:boolean, it accepts any of these four literal
strings.
<!-- All four representations are valid xs:boolean elements -->
<isActive>true</isActive>
<isActive>1</isActive>
<isPending>false</isPending>
<isPending>0</isPending>Why XML Schema Accepts 1, 0, true, and false
The decision by the W3C to permit both textual (true,
false) and numeric (1, 0)
literals was driven by technical and practical integration needs:
1. Cross-Language and Database Interoperability
Different computing platforms and legacy systems represent boolean
logic differently. High-level languages like Java, C#, and JavaScript
traditionally use literal true and false
keywords. Conversely, low-level languages (like C) and many relational
database systems (such as MySQL or Oracle) store booleans as numeric
flags (1 for true, 0 for false, or via
BIT/TINYINT types). Permitting both formats in
XSD removes the need for data transformation layers when serializing and
deserializing data across heterogeneous environments.
2. Performance and Compactness
Using 1 and 0 provides a more compact data
representation within raw XML payloads, saving bandwidth in
high-throughput or resource-constrained environments. Additionally,
processing single-digit integers often requires less computational
parsing overhead than processing multi-character strings.
3. Human Readability vs. Machine Processing
Textual representations (true, false)
prioritize human readability and semantic clarity when configuration
files or documents are inspected manually. Numeric representations
(1, 0) prioritize machine-centric operations.
Allowing both satisfies both use cases.
Canonical Representation
While 1, 0, true, and
false are all valid in the lexical space, the W3C specifies
that the canonical lexical representation for the value true is
the literal string true, and for false is the
literal string false.
When an XML processor performs canonicalization (such as XML-C14N for
digital signatures) or schema-aware serialization, numeric values
1 and 0 are standardly converted to their
canonical string equivalents: true and
false.