"Default values of both attributes and elements are declared using the
default attribute, although this attribute has a slightly different consequence in each case. When an attribute is declared with a default value, the value of the attribute is whatever value appears as the attribute's value in an instance document; if the attribute does not appear in the instance document, the schema processor provides the attribute with a value equal to that of the
default attribute. Note that default values for attributes only make sense if the attributes themselves are optional, and so it is an error to specify both a default value and anything other than a value of
The schema processor treats defaulted elements slightly differently. When an element is declared with a default value, the value of the element is whatever value appears as the element's content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the
default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.
fixed attribute is used in both attribute and element declarations to ensure that the attributes and elements are set to particular values. For example,
po.xsd contains a declaration for the
country attribute, which is declared with a
US. This declaration means that the appearance of a
country attribute in an instance document is optional (the default value of
optional), although if the attribute does appear, its value must be
US, and if the attribute does not appear, the schema processor will provide a
country attribute with the value
US. Note that the concepts of a fixed value and a default value are mutually exclusive, and so it is an error for a declaration to contain both
Liquid XML Data Binder
The Liquid XML Data Binder for .Net uses nullable simple types. As such it initialises element and attribute values with the Default/Fixed value so they are always written out to the output XML document even if they are set as optional in the XSD. The alternative to this would be to introduce additional attributes to hold default values which would make the generate API difficult to use.
C++, Java and Legacy .Net
The Liquid XML Data Binder for languages where IsValid properties are generated for Optional items do not initialise element and attribute values with the Default/Fixed value. As such, these are not written out to the output XML document unless the SerializationContext property WriteDefaultElementValues is set to true.
The SerializationContext has two properties to control the XML output for Default/Fixed values:
NoOptionalAttributeDefaultOutput - if set true, no optional attributes with default values are written out. (Default is false)
WriteDefaultElementValues - if set false, empty elements for default values are written out, i.e. any processor reading the XML should then know to use the default values. (Default is false)