fwshngtn
fwshngtn

Reputation: 45

Import same Namespace from different xsd

I need some help building a xsd. A third party provides a xsd (v1.xsd), which I have to include in my custom xsd (example.xsd). They will soon release a new version of their xsd (v2.xsd), unfortunatly using the same namespace. I need to include the new version (v2.xsd) as well as the old version (v1.xsd) in my xsd, but im struggling to achive this. I have no clue how to import these two (or multiple) xsd and referencing them, using the same namespace.

Does anybody have an advice how I can do this?

I hope this little example shows, what I am trying to accomplish.

example.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:v1="http://loremipsum.org/xsd/1_1"
        xmlns:v2="http://loremipsum.org/xsd/1_1"
>

    <xs:import namespace="http://loremipsum.org/xsd/1_1"
               schemaLocation="include/v1.xsd"/>

    <xs:import namespace="http://loremipsum.org/xsd/1_1"
               schemaLocation="include/v2.xsd"/>

    <xs:element name="CONTENT">
        <xs:complexType>
            <xs:sequence>
                <xs:choice>
                    <xs:element ref="v1:note"/>
                    <xs:element ref="v2:note"/>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

v1.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://loremipsum.org/xsd/1_1"
           targetNamespace="http://loremipsum.org/xsd/1_1"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

    <xs:element name="note">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="to" type="xs:string"/>
                <xs:element name="from" type="xs:string"/>
                <xs:element name="heading" type="xs:string"/>
                <xs:element name="body" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema> 

v2.xsd

<?xml version="1.0"?>
<xs:schema
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns="http://loremipsum.org/xsd/1_1"
        targetNamespace="http://loremipsum.org/xsd/1_1"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified">

    <xs:element name="note">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="to" type="xs:string"/>
                <xs:element name="from" type="xs:string"/>
                <xs:element name="additional" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

Any help is appreciated :).

Upvotes: 0

Views: 1236

Answers (1)

martijn
martijn

Reputation: 495

You can't without changing your customers xml schemas. The namespace makes the element unique. If the element "note" is defined in both v1 and v2 and they share the same namespace the parser does not understand which should be used. The best option is to convince your customer to user versioning on the xsd and otherwise merge the v1 and v2 like below.

example.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:v2="http://loremipsum.org/xsd/1_1">
    <xs:import namespace="http://loremipsum.org/xsd/1_1" schemaLocation="v2.xsd"/>
    <xs:element name="CONTENT">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="v2:note"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

v2.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://loremipsum.org/xsd/1_1" targetNamespace="http://loremipsum.org/xsd/1_1" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="note">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="to" type="xs:string"/>
                <xs:element name="from" type="xs:string"/>
                <xs:choice>
                    <xs:sequence>
                        <xs:element name="additional" type="xs:string"/>
                    </xs:sequence>
                    <xs:sequence>
                        <xs:element name="heading" type="xs:string"/>
                        <xs:element name="body" type="xs:string"/>
                    </xs:sequence>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Upvotes: 1

Related Questions