Default and Fixed values

Default can be a misleading term in XSD. The following explanation is taken from the W3C document XML Schema Part 0: Primer Second Edition

"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 optional for use.

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.

The 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 fixed value US. This declaration means that the appearance of a country attribute in an instance document is optional (the default value of use is 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 fixed and default attributes."

Liquid XML Data Binder 
 
.Net
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 they are set via the API.
 
SerializationContext
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 the know to use the default values. (Default is false)
 
 

Add Feedback