[xsd-users] optional-boolean with default values

Boris Kolpackov boris at codesynthesis.com
Wed Aug 6 08:16:14 EDT 2008


Hi Erkan,

Erkan Kaya <raxody at yahoo.com> writes:

> What I'd like to ask is why doesn't xsd geneate the code like below:
> 
> <xs:element name="XXX" type="xs:boolean" default="false" minOccurs="0" />
> 
> 
> it has all the neccessary information to correctly create a class that 
> would:
> 
> if XXX exists in the config
>    return what it reads from config
> else
>    return the default value

We cannot do this because that's not how elements with default values
work in XML Schema. For attributes it is straightforward in that if
an optional attribute is not present then the default value is used.

With elements, things are more complicated. An optional element with
a default value can be in three states: 

1. present with custom value:      <XXX>true</XXX>
2. present with default value:     <XXX></XXX>
3. not present

That is, for an element to assume the default value it should be
present but empty. I think these rules make elements with default
values quite inconvenient to use and that's the reason why they
are not very popular (unlike attributes with default values).

BTW, the table in Appendix A, "Default and Fixed Values" in the
C++/Tree Mapping User Manual summarizes the rules for default and
fixed values in elements and attributes with various cardinalities:

http://codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#A

Boris




More information about the xsd-users mailing list