Let's say I want to set up a generic complexType like so:
<xs:complexType name="button">
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="href" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="label" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
And I want to reference that complexType in various places in my schema file like so:
<xs:element name="someButton" type="button" />
Is it possible for me to set default values for the button sub-elements through the someButton element? (I.e. if I want someButton to have a default label of "Go" or a default href of "index.html")
Basically... right now I have something like
<Field Name="State" DataSourceField="State" />
and I'm trying to remove the redundancy in as simple a manner as possible.
No, only for simple values. But maybe you can use them to do what you want, by giving default values for all the simple parts of your complex Type. However, it works better for attributes than for the elements you have (because "Default attribute values apply when attributes are missing, and default element values apply when elements are empty" - see below). Attributes are themselves optional by default:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="button" type="button"/>
<xs:complexType name="button">
<xs:attribute name="id" type="xs:string"/>
<xs:attribute name="href" type="xs:string" default="index.html"/>
<xs:attribute name="label" type="xs:string" default="Go"/>
</xs:complexType>
</xs:schema>
<button id="1"/>
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. [emphasis added]
http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With