[xsd-users] default value xsd and parsing

Boris Kolpackov boris at codesynthesis.com
Fri Feb 26 10:06:42 EST 2010


Hi Benjamin,

Benjamin Schindler <bschindler at inf.ethz.ch> writes:

> I have the following thing I'd like to achieve:
> 
> 	<xs:complexType name="ServerConfiguration_t">
> 		<xs:sequence>
> 			<xs:element name="port" type="xs:int" default="5454"/>
> 		</xs:sequence>
> 	</xs:complexType>
> 
> (I tried the same with xs:all instead of sequence)
> 
> When I get an xml that doesn't have the port attribute, validating the
> xml fails. But I'd expect that I get an object without the xsd::Optional
> template which has the default value 5454 if the xml does not have it.

While you say "port attribute", in the schema you have an element and
that makes all the difference. The XML Schema spec says that for an
element to get a default value, an empty element should be present
in XML, as in:

<port/>

In other words, omitting an element from the XML completely doesn't
result in it having a default value. Rather, if this element is 
optional, it is treated as no being present and if it is required,
as in your case, then you will get a validation error. This behavior
makes attributes with default value an odd construct which is not 
used much.

On the other hands, for attributes, the default value is used if the
attribute is not present, and this is used a lot in schemas.

There is a table in Appendix A, in the C++/Tree Mapping User Manual
which summarizes the differences between default values for elements
and attributes:

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

Boris



More information about the xsd-users mailing list