Is it possible to have multiple inheritance in XML schemas, if so how?
Example
<xs:schema xmlns:xs="http://www.w3.org/2001/Schema" ....>
<xs:complexType name="Account">
<xs:sequence>
<xs:element name="balance" type="xs:decimal"/>
<xs:element name="accountNo" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="CreditCardAccount">
<xs:complexContent>
<xs:extension base="Account">
<xs:sequence>
<xs:element name="expiryDate" type="xs:date"/>
<xs:element name="issuer" type="xs:string"/>
<xs:element name="type" type="xs:string" use="required"/>
</xs:sequence>
</xs:extension>
<xs:complexContent>
</xs:complexType>
</xs:schema>
My question is; Is it possible for CreditCardAccount to inherit from multiple types instead of Account only?
Schemas can be composed of one or more XML documents. These schema documents can be explicitly joined together using the include and import elements.
The inheritance features in XML Schema allow you to create type hierarchies that capture the data models of the underlying software systems that XML is designed to support.
Supported XML Schema data types The monitor model supports eight XML Schema data types.
Something that resembles multiple inheritance in some ways can be managed using named model group and named attribute groups; whether it resembles multiple inheritance in the way you want is something only you can tell.
Suppose you have complex types Account (as shown above) and Customer, and you'd like complex type CreditCardAccount to inherit from each of them. As others have pointed out, you can't specify two base types in a complex type definition. But you can package the content model of the Account and Customer types into named model groups, and refer to the appropriate named group from the type definition.
<xs:group name="Account">
<xs:sequence>
<xs:element name="balance" type="xs:decimal"/>
<xs:element name="accountNo" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:group name="Customer">
<xs:sequence>
<xs:element name="custid" type="xs:int"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="phone" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:complexType name="Account">
<xs:sequence>
<xs:group ref="Account"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Customer">
<xs:sequence>
<xs:group ref="Customer"/>
</xs:sequence>
</xs:complexType>
Then you can refer to both named groups from CreditCardAccount:
<xs:complexType name="CreditCardAccount">
<xs:sequence>
<xs:group ref="Account"/>
<xs:group ref="Customer"/>
<xs:element name="expiryDate" type="xs:date"/>
<xs:element name="issuer" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
</xs:sequence>
</xs:complexType>
An XSD validator will not regard the types Customer, Account, and CreditCardAccount as related in this case, which may or may not matter.
Of course, a simpler approach would be to use elements as packages of information, and define elements Account, Customer, and CreditCardAccount, with CreditCardAccount containing (surprise) an Account element and a Customer element.
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