Joao
Joao

Reputation: 7486

XSD - Override element

I've a base class, marked as abstract:

<!-- TYPE: BASE CLASS -->
<xs:complexType name="BaseClass" abstract="true">
    <xs:sequence>
        <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
        <xs:element name="implementation" type="BaseClass" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>

Now the class that inherits BaseClass and overrides 'implementation' with null:

<xs:complexType name="MyClass">
    <xs:complexContent>
        <xs:extension base="BaseClass"/>
    </xs:complexContent>
</xs:complexType>

I've tried this but is invalid:

<xs:complexType name="MyClass">
    <xs:complexContent>
        <xs:extension base="BaseClass">
            <xs:sequence>
                <xs:element name="implementation" fixed="xs:nil"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

Is there a way to do this?

Upvotes: 1

Views: 4568

Answers (1)

tom redfern
tom redfern

Reputation: 31770

Marking something as abstract in XSD schema basically means that it cannot appear in an instance document. It should be used with the substitutionGroup attribute.

For example

<xs:element name="replaceme" abstract="true" /> 
<xs:element name="replacement1" substitutionGroup="replaceme" />
<xs:element name="replacement2" substitutionGroup="replaceme" />

This means that in an instance document you would be forced to use either a "replacement1" or a "replacement2" element, ie the xsd is forcing you to substitute the "replaceme" element with one of the replacements from the substitution group.

So to achieve what you want you need to create a substitution group consisting of the types you want to use instead of your base type. I am not sure this is possible with complex types but give it a go.

Upvotes: 1

Related Questions