If I have an extension, how can I assure that the derived elements are in front of the base class elements? The default is the other way around. I would have loved to use all
, but I know that is impossible.
<xs:complexType name="BaseClass">
<xs:sequence>
<xs:element name="foo" type="xs:string/>
<xs:element name="bar" type="xs:string/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="DerivedClass">
<xs:complexContent>
<xs:extension base="BaseClass">
<xs:sequence>
<!-- This makes the order foo, bar, cheese, tomato -->
<!-- But I need cheese, tomato, foo, bar -->
<xs:element name="cheese" type="xs:string/>
<xs:element name="tomato" type="xs:string/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="BaseClass" type="BaseClass"/>
<xs:element name="DerivedClass" type="DerivedClass" substitutionGroup="BaseClass"/>
The xml I would like to be accepted looks something like this:
<mylist>
<BaseClass>
<foo>lala</foo>
<bar>lala</bar>
</BaseClass>
<DerivedClass>
<cheese>cheddar</cheese>
<tomato>red</tomato>
<foo>lala</foo>
<bar>lala</bar>
</DerivedClass>
</mylist>
At the moment I'm thinking of just copying all elements of BaseClass
into DerivedClass
as well, but I don't know what happens with substitutiongroups and what not.
In the most general sense, XML element order does not matter, unless otherwise specified by the appropriate schema.
Right-click the XML Schema file (file extension *. xsd ) that you want to open, and select Open. The XML Schema file opens in the XML Schema editor.
According to the XML specification, the order of attribute specifications in a start-tag or empty-element tag is not significant.
The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.
If I have an extension, how can I assure that the derived elements are in front of the base class elements?
Unfortunately, it is impossible. When a complexType is extended, the new elements are added to the base elements as sequence. Basically, the new content model will looks as follows:
(base element model), (extension element model)
That's how the type extension in XSD works. You may wonder why? Because the extension type must comply with the base type. You cannot have anything as an extension of anything as a base.
Suppose you have a software that knows how to process elements of type A. The software may assume that actually the type A might be extended, but in any case, it must be able to find in an element supposed to be of type A everything it knows/expect from type A... and the element content inherent to type A (which is one of the most important things) must come the first!
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