Is it possible to define a default value for a missing element in an XML Schema. I have the following snippet:
<xs:element name="protocol" nillable="false" minOccurs="0" default="ftp">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="ftp"/>
<xs:enumeration value="webdav"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
If I have in the XML file
<protocol>ftp</protocol>
or
<protocol>webdav</protocol>
it validates and I obtain the right value. If I have in the XML file
<protocol></protocol>
it also validates and I obtain the default value of ftp.
My searches show that default attribute values apply when attributes are missing, and default element values apply when elements are empty. Is it possible to have a default value for a missing element?
Regards
rambius
A default value is automatically assigned to the element when no other value is specified. A fixed value is also automatically assigned to the element, and you cannot specify another value. If I add an element in the XML without value it can have a default value.
The default value for both the minOccurs and the maxOccurs attributes is 1. Thus, when an element such as comment is declared without a maxOccurs attribute, the element may not occur more than once.
The default is false. If nillable is true, this enables an instance of the element to have the nil attribute set to true. The nil attribute is defined as part of the XML Schema namespace for instances.
The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.
No. XSD doesn't provide for that.
You can specify the default value of an element. But once it is missing (when that's allowed by the content model of its parent), any requests to that element will return either empty string or null (or just an error). The missing element is non-existent element!
For attributes it is possible, because attributes are far simpler. All attributes of an element effectively constitute an unordered set of named simple values. There is no some kind of attribute tree (with the variable structure at that) attached to the parent element.
But with elements things are far more complex. If something "default" about missing elements were allowed, that would cause lots of ambiguities. For instance, some kind of "default content" would have to be specified then, which would be some sequence of elements evoked automatically in place of emptiness... or even a number of possible "default contents" each evoked when only some elements are specified explicitly and others must supplement them implicitly (by default). .... Well, if you think further on, the things are getting mind-boggling complex. Another language would have to be created then! But for what purpose?
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