Search code examples
xmlxsd

Import two XSDs from namespace 2 with non-overlapping definitions into an XSD from namespace 1


I refer to the question 'Import same Namespace from different xsd' with the difference that the definitions in my two imported XSDs do not overlap.

I need to import two XSDs (kd1.xsd and kd2.xsd) with identical namespace and non-overlapping element or type definitions into a parent XSD (main.xsd) with its own namespace. The main.xsd is read in by applications, class generators and validators, for example.

The XSDs to be imported use both definitions from another XSD (other.xsd), which is in the same namespace as the main.xsd. However, I think this has nothing to do with the problem.

My problem is that the definitions of the second imported kd2.xsd are apparently not visible.

main.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.anbieter.de/xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:kun="http://www.anbieter.de/xsd"
    targetNamespace="http://www.anbieter.de/xsd"
    xsi:schemaLocation="http://www.anbieter.de/xsd other.xsd
        http://www.kunde.de/xsd kd1.xsd
        http://www.kunde.de/xsd kd2.xsd">

    <xs:include schemaLocation="other.xsd"/>
    <xs:import namespace="http://www.kunde.de/xsd" schemaLocation="kd1.xsd"/>
    <xs:import namespace="http://www.kunde.de/xsd" schemaLocation="kd2.xsd"/>
</xs:schema>

kd1.xsd, kd2.xsd

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

    <xs:element name="element1">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="sub11" type="xs:string"/>
                <xs:element name="sub12" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

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

    <xs:element name="element2">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="sub21" type="xs:string"/>
                <xs:element name="sub22" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

other.xsd:

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

    <xs:element name="otherelement">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="foo" type="xs:string"/>
                <xs:element name="bar" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

For example, a validator that pulls in the main.xsd sees the definition of element1 in kd1.xsd but not that of element2 in kd2.xsd ('Can't find the declaration of ...').

<?xml version="1.0" encoding="UTF-8"?>
<kun:element1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.anbieter.de/xsd"
 xmlns:kun="http://www.kunde.de/xsd"
 xsi:schemaLocation="http://www.kunde.de/xsd file:/C:/Users/uheyder/Downloads/main.xsd">
    <kun:sub11>sub110</kun:sub11>
    <kun:sub12>sub120</kun:sub12>
</kun:element1>

is ok but

<?xml version="1.0" encoding="UTF-8"?>
<kun:element2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.anbieter.de/xsd"
 xmlns:kun="http://www.kunde.de/xsd"
 xsi:schemaLocation="http://www.kunde.de/xsd file:/C:/Users/uheyder/Downloads/main.xsd">
    <kun:sub21>sub110</kun:sub21>
    <kun:sub22>sub120</kun:sub22>
</kun:element2>

is not. If I swap the import lines, it is the other way around.

Similarly, I can generate examples of element1 with the XML example generator of a used XML development tool, but not of element2.

Question 1: Is it generally not allowed and possible to import two XSDs with identical namespaces and non-overlapping element or type definitions directly into a parent XSD with its own namespace, so that the parent XSD alone can be worked with?

Question 2: If there are options, how do I need to structure my XSDs differently or change content?

Constraints are that

  • the namespaces of kd1.xsd and kd2.xsd are identical

  • this differs from the namespace of main.xsd and

  • only the main.xsd is used as anchor

As an emergency solution, I would import an additional auxiliary XSD in the namespace of both kd1.xsd and kd2.xsd, which in turn includes kd1.xsd and kd2.xsd. However, I find this solution cumbersome and less transparent.

Note: The example is very simplified, but leads to the same problems as the actual XSD model.

Thanks, Ulf


Solution

  • In general, a schema that imports the same namespace from two different locations isn't interoperable: different schema processors will handle it differently. I don't think you've said which schema processor you are using, so I can't give you a product-specific answer. Some schema processors provide options to control this, for example Saxon has a configuration option MULTIPLE_SCHEMA_IMPORTS.

    The best way to achieve interoperable results is to import the namespace from a single stub document that contains xs:include declarations for the multiple "real" schema documents: your "emergency solution".