XML / XSD 'extension' of an element always has undefined complexType

Mciccarone picture Mciccarone · Apr 11, 2012 · Viewed 7.9k times · Source

I am attempting to write an XSD Schema that extends a ComplexType that is defined in an Element.

I am attempting to use the Notepad++ XMLTools plugin to troubleshoot the issue, but I always get "Unable to Parse Schema file." with no description of the error, so I have been using the Validator located here to get more details:

http://www.xmlforasp.net/schemavalidator.aspx

The Output I get from this is:

Status: Undefined complexType 'http://test.org:BaseClass' is used as a base for complex type extension.

I've tried removing the :test namespace in the xs:schema tag, I've tried removing the test: namespace qualifier from the ref within ClassHierarchy, and I've tried adding the namespace to the element definitions, but I cannot get the schema to pass validation.

Any help would be greatly appreciated! Thanks

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

    <xs:element name="BaseClass">
        <xs:complexType>
            <!-- More referneces in here (Meaning this must be a complex type) but removed out to make a bit simpler -->
            <xs:attribute name="test_name" type="xs:string" use="required"/>
            <xs:attribute name="second_name" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="SubClass">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="test:BaseClass">
                    <xs:attribute name="min_value" type="xs:float" default="0.0"/>
                    <xs:attribute name="max_value" type="xs:float" default="1.0"/>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>


    <xs:element name="ClassHierarchy">
        <xs:complexType>
            <xs:sequence>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="test:BaseClass" minOccurs="0" maxOccurs="unbounded"/>
                    <xs:element ref="test:SubClass" minOccurs="0" maxOccurs="unbounded"/>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The XML File Contains:

<?xml version="1.0"?>
    <ClassHierarchy
    xmlns="http://test.org"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://test.org Test.xsd">
        <BaseClass test_name="Test1" />
        <SubClass test_name="Test2" min_value="0.0" max_value="1.0" />
    </ClassHierarchy>

Answer

Dmitry Reznik picture Dmitry Reznik · Apr 11, 2012

The thing is you're trying to set base type value to an element. But the "type" is expected. You should make it more like this:

<xs:element name="BaseClass" type="BaseType" />

<xs:complexType name="BaseType">
      ...
</xs:complexType>

<xs:complexType name="SubType">
   <xs:complexContent>
       <xs:extension base="BaseType">
           ....
       </xs:complexContent>
</xs:complexType>