Xml schema extension order

The only way around this is to use groups, which can be inserted at any point in a sequence. However, with this approach you can't use substitutionGroup.

<xs:group name="BaseGroup">
  <xs:sequence>
    <xs:element name="foo" type="xs:string"/>
    <xs:element name="bar" type="xs:string"/>
  </xs:sequence>
</xs:group>
<xs:complexType name="BaseClass">
  <xs:group ref="BaseGroup"/>
</xs:complexType>
<xs:complexType name="DerivedClass">
  <xs:sequence>
    <xs:element name="cheese" type="xs:string"/>
    <xs:element name="tomato" type="xs:string"/>
    <xs:group ref="BaseGroup"/>
  </xs:sequence>
</xs:complexType>
<xs:element name="BaseClass" type="BaseClass"/>
<xs:element name="DerivedClass" type="DerivedClass"/>

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!