XSD: default value of missing item

Is it possible to determine the 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 an XML file

 <protocol>ftp</protocol> 

or

 <protocol>webdav</protocol> 

It checks and I get the correct value. If I have an XML file

 <protocol></protocol> 

it also checks and I get the default value for ftp.

My searches show that attribute defaults 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 item?

Hi

rambius

+8
xml xsd
source share
2 answers

Not. XSD does not provide for this.

You can specify a default value for an item. But as soon as it is absent (when its parent's content model is allowed), any requests to this element return either an empty string or zero (or just an error). A missing element is a nonexistent element!

For attributes, this is possible because attributes are much simpler. All element attributes effectively represent an unordered set of named simple values. There is no attribute tree (with one or another structure) attached to the parent element.

But with elements, things are much more complicated. If something "defaulted" about missing items was allowed, this will cause a lot of ambiguity. For example, some “default content” should be indicated, which would be some sequence of elements called automatically instead of emptiness ... or even the number of possible “default values”, each of which is called when only some elements are explicitly specified, and others should add them implicitly (by default). .... Well, if you think next, everything becomes a stunning complex. Then another language will be created! But for what purpose?

+18
source share

call this function

 def _get_(x): if x is not None: return(x.text) else: # print('Setting Blank') return '' _get_(parent.find('childtag')) 
0
source share

All Articles