If you have an XML schema which dictates an element has a default value, and then you have an XML file following that schema which completely omits that element is it still valid?
i.e. if the element is missing does the validator just say ok the element is missing so we take the default value defined in the schema and the XML is valid?
So maybe something like:
<xs:element name="test" type="xs:boolean" default="false"/>
Then an XML file that misses out the 'example' element all together, is that valid?
The reason I ask is because I've seen many schemas with elements using the attribute: minOccurs="0"
which infers that if those elements are missing then it 开发者_运维技巧will still validate. My question is will it validate if minOccurs
is not specified but there is a default value specified instead?
Thanks.
No, omitting an element that does not have minOccurs="0"
is invalid -- even if there is a default
specified on that element.
In XML Schema, elements can have defaults! Most people are familiar with defaults on attributes (which is a feature also available in DTDs), but defaults on elements are less commonly seen.
With elements, the default value only gets used if the element is present and empty. Which is different from attributes where the default value gets used when the attribute is not present.
Take this example:
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="xs:boolean" default="false"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The following instance is not schema valid, because there must be one test
element. The default makes no difference, because there is no test
element.
<foo></foo>
The following instance is schema valid and the test
element takes on the default value (in this case it is "false") because the test
element is present and is empty.
<foo><test></test></foo>
Note: if the XML Schema did not specify a default, then both of the above example instances would obviously not be schema valid. In the first example, there is no test
element when there needs to be exactly one of them. In the second example, there is a test
element but its contents is empty, and an empty string is not a valid value for an xs:boolean
.
minOccurs
and default
refer to two different concepts. Your question needs more context to be answered completely. minOccurs
refers to the number of times an element can occur as the child of another. default
refers to the string value (sometimes typed - e.g. here as boolean). Your use of default
on element is invalid
.
Here is default for attributes (from w3schools)
Default for Attributes
Attributes may have a default value specified.
A default value is automatically assigned to the attribute when no other value is specified.
In the following example the default value is "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
and for minOccurs
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.
default
refers to a string value in an attribute. minOccurs
means you can omit an element.
精彩评论