Since several years, I have used this architecture in my models.
I have 3 models:
- object which is the main model,
- A and B which extend the object model and add new elements.
object.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="A.xsd"/>
<xs:include schemaLocation="B.xsd"/>
<xs:complexType name="object">
<xs:sequence>
<xs:element name="elt1" type="xs:token"/>
</xs:sequence>
</xs:complexType>
<xs:element name="object" type="object"/>
</xs:schema>
A.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="object.xsd"/>
<xs:complexType name="A">
<xs:complexContent>
<xs:extension base="object">
<xs:sequence>
<xs:element name="elt2" type="xs:token"/>
<xs:element name="elt3" type="xs:token"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
B.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="object.xsd"/>
<xs:complexType name="B">
<xs:complexContent>
<xs:extension base="object">
<xs:sequence>
<xs:element name="elt4" type="xs:token"/>
<xs:element name="elt5" type="xs:token"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
I use the xsi:type in the example to choose if the object is of type A or B. Here is the example associated to A:
<?xml version="1.0" encoding="UTF-8"?>
<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="A">
<elt1>test1</elt1>
<elt2>test2</elt2>
<elt3>test3</elt3>
</object>
Some of my partnairs import the XSD schemas in their ERP to create their class objects. However they have to adapt the files I provide to import them correctly and to be able to exploit them.
I wonder if I use the xsi:type correctly. Indeed, there is no reference to the xsi namespace (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance") in the XSD. It is the same with the xsi:type reference, how their ERP can know to add it?
Yes you are using xsi:type correctly. (It's not a design I greatly like, but that's irrelevant.)
You don't need to declare the xsi namespace (or prefix) in your schema, only in your instance document.
One reason I don't greatly like this design is that users of this schema have to understand it in order to create instance documents, that is, they need to understand that the instance document needs to contain an xsi:type attribute (and therefore to bind the "xsi" prefix).