tags:

views:

20

answers:

1

Hello.

This is a part of my xml schema

<xs:complexType name="Friend">
    <xs:all>
        <xs:element name="name" type="xs:string" />
        <xs:element name="phone" type="xs:string" />
        <xs:element name="address" type="xs:string" />
    </xs:all>
</xs:complexType>

<xs:complexType name="Coworker">
    <xs:all>
        <xs:element name="name" type="xs:string" />
        <xs:element name="phone" type="xs:string" />
        <xs:element name="office" type="xs:string" />
    </xs:all>
</xs:complexType>

For better maintainability, I would like to have the shared attributes in an (abstract) super type or something like that. But more important, I want that all elements are unordered and also optional.

Is this possible, and what is the best way to do it?

A: 

You have to limit yourself a little bit, some of the things you are trying to do are not possible in XML Schema.

Suppose you introduce a complex type called Person to be a super-type of Friend and Coworker. Here are your options:

  1. Replace xs:all with xs:sequence, renove name and phone from the sub-types, add to the super-type, and add inheritance. Your elements now have to be ordered, but you can make them individually optional. It is illegal to use xs:all in type hierarchies in XML Schema, because the processor cannot tell where the parent content model stops and the child content model starts.
  2. Replace xs:all with <xs:choice maxOccurs="unbounded"> in both types, and add your inheritance. Then your elements become unordered again, but they may repeat.

So in conclusion: given your type names up there, I would guess that your requirements will not be exactly met. I would go for the first option: insisting on arbitrary element order is often not as useful as it seems.

xcut
I was afraid this would be the case. So (1) it will be.
Arian