import in XSD and its implementation

493 views Asked by At

I have 2 separate XSDs with some common attributes. I want to create another XSD and put all my common attributes in the separate XSD and import them in the 2 XSDs i have already rather than repeating them or duplicating them in both the XSDs.

Is there any reference for such implementation?

1

There are 1 answers

0
RoboJ1M On

We do it like this:

Shared "library" xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.common.namespace/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.common.namespace/">
    <xs:attribute name="ACommonAttribute" type="xs:float" default="1.7"/>
</xs:schema>

Left xsd with same target namespace:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.common.namespace/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.common.namespace/">
    <xs:include schemaLocation="Common.xsd"/>
    <xs:element name="MyLeftElement">
        <xs:annotation>
            <xs:documentation>Comment describing your root element</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:attribute ref="ACommonAttribute"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

Right xsd with different target namespace (needs an Import instead if an Include statement)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://another.namespace/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:libs="http://www.common.namespace/" targetNamespace="http://another.namespace/">
    <xs:import namespace="http://www.common.namespace/" schemaLocation="Common.xsd"/>
    <xs:complexType name="RightComplexType">
        <xs:sequence>
            <xs:element name="Bit">
                <xs:complexType>
                    <xs:attribute ref="libs:ACommonAttribute"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>