Search code examples
xmlxsdxml-namespacesxsd-validationxml-validation

XSD problem importing another namespace and using its type declarations


I am getting used to XML Schemas and tried to import another schema into my own.

The initial schema file test.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
  targetNamespace="test"
  xmlns="test"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  elementFormDefault="qualified" >

    <xs:complexType name="Test">
        <xs:all>
            <xs:element name="test-import" minOccurs="0" type="xs:string" />
        </xs:all>
    </xs:complexType>

</xs:schema>

The importing schema test2.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
  targetNamespace="test2"
  xmlns="test2"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:test="test"
  elementFormDefault="qualified" >

    <xs:import namespace="test" schemaLocation="./test.xsd" />

    <xs:element name="project">
        <xs:complexType>
            <xs:sequence>

                <xs:element name="test" type="test:Test" />

                <xs:element name="test2" type="Model" />
                
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="Model">
        <xs:all>
            <xs:element name="model-test" minOccurs="0" type="xs:string" />
        </xs:all>
    </xs:complexType>

</xs:schema>

And finally the test.xml file which imports the namespace test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<file xmlns="test2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="./test2.xsd">

    <test>
        <test-import>Text</test-import>
    </test>
    
    <test2>
        <model-test>Text</model-test>
    </test2>

</file>

In the end the tag test2 would work fine but at <test-import>Text</test-import> i would get the error message:

 - test-import

One of the following is expected:
 - test-import

Error indicated by:
 {test}
with code:xml(cvc-complex-type.2.4.a)```


Solution

  • Too many issues to list out, but below you can find all mistakes corrected such that your XML will validate successfully against your XSD.

    See also

    File set with all errors corrected

    test.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="https://example.com/test2" 
             xmlns:test="https://example.com/test"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://example.com/test2 test2.xsd">
      <test>
        <test:test-import>Text</test:test-import>
      </test>
      <test2>
        <model-test>Text</model-test>
      </test2>
    </project>
    

    test2.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema 
        targetNamespace="https://example.com/test2"
        xmlns="https://example.com/test2"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:test="https://example.com/test"
        elementFormDefault="qualified" >
    
      <xs:import namespace="https://example.com/test" schemaLocation="test.xsd" />
    
      <xs:element name="project">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="test" type="test:Test" />
            <xs:element name="test2" type="Model" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    
      <xs:complexType name="Model">
        <xs:all>
          <xs:element name="model-test" minOccurs="0" type="xs:string" />
        </xs:all>
      </xs:complexType>
    
    </xs:schema>
    

    test.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema 
        targetNamespace="https://example.com/test"
        xmlns="https://example.com/test"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        elementFormDefault="qualified" >
    
      <xs:complexType name="Test">
        <xs:all>
          <xs:element name="test-import" minOccurs="0" type="xs:string" />
        </xs:all>
      </xs:complexType>
    
    </xs:schema>