Re: Restrictions on booleans

Hi Robin,

> xsd:boolean accepts the values "true", "false", "1", and "0".

That's right -- those are the legal "lexical representations" of
xs:boolean. But xs:boolean only has two values, true and false ("true"
and "1" are lexical representations of the value true; "false" and "0"
are lexical representations of the value false).

> My reading of the spec would seem to indicate that the following is
> legal:
>
> <restriction base='boolean'>
>    <pattern value='true|false'/>
> </restriction>
>
> in order to model a language that has boolean types but only those two 
> strings to flag truth (0 and 1 being illegal).

Quite right.

> My questions are: have I understood the spec correctly, and if so
> does that make much sense? It feels a bit strange having to use a
> pattern here, any reason why the equivalent enumeration isn't
> allowed?

I'm not sure why the enumeration isn't allowed at all, but note that
if it were you can only constrain the *values* that are permitted
through enumeration. If you did:

  <xs:restriction base="xs:boolean">
    <xs:enumeration value="true" />
    <xs:enumeration value="false" />
  </xs:restriction>

you'd be saying "a boolean value, but only the *values* true or
false", not "a boolean value, but only the *lexical representations*
"true" or "false"". In other words, using an enumeration wouldn't
enable you to say that you wanted to permit "true" and "false" but not
"1" and "0".

It's just the same as with numbers, you can enumerate:

  <xs:restriction base="xs:decimal">
    <xs:enumeration value="5" />
    <xs:enumeration value="10" />
  </xs:restriction>

but that doesn't stop someone using "00005" or "10.0000" in the XML
document.

If enumeration were allowed for xs:boolean, the only use would be to
say that you only wanted to allow true values ("true" or "1") or only
wanted to allows false values ("false" or "0"). I imagine that this is
why enumerations aren't allowed for booleans -- it's not something
that you're actually likely to want to do.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Thursday, 22 August 2002 09:50:40 UTC