Inheritance and XML Schemas
Often you need to employ OO patterns when solving integration problems using XML.
Inheritance is especially useful when you are looking to abstract commonality within a base type. Thus ensuring less redundant code and reuse…
The W3C XSD syntax allows you to create complex types (complexType), import schemas (import), refer to imported schemas using namespaces and inherit types from other types.
Here’s a sample XSD that contain two types, one that derives from the other.
Note that both the Person and Company derive from Entity, using <xs:extension> within the <xs:complexContent> tag.
The following picture illustrates the Entity -> Person "Kind of" relationship:
You can also separate the base types into its physical XSD file and import that within other schemas. To accomplish that you will need to:
1. In the specialised schema, use the <xs:import>to import the base schema as follows:
<xs:import id="BaseTypes" namespace="http://somedomain.com/BaseTypes.xsd" schemaLocation="BaseTypes.xsd" />
2. Specify a logical namespace for the imported schema so that it can be reference from code as follows:
<xs:schema xmlns:bt="http://somedomain.com/BaseTypes.xsd"/>
There’s one caveat to having related complex types in separate xsd files. The Microsoft .Net xsd code generator craps out while creating transport classes for these types.
Let me know if you figure that out!
Filed under: Technology