views:

16

answers:

1

In the schema below, the line <xs:element type="cmn:AddressType" name="ResidentialAddress" minOccurs="1" maxOccurs="1" /> gives an error Type 'http://company.com/Common:AddressType' is not declared.

Does anyone know why? It shows in the Visual Studio 2008 editor, and also if I try validate an XML file with XDocument.

Schema Student.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Student"
    xmlns:cmn="http://company.com/Common"
    targetNamespace="http://company.com/Student"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

  <xs:import id="cmn" schemaLocation="Address.xsd" 
             namespace="http://company.com/Common" />

  <xs:element name="Student" nillable="false">
    <xs:complexType>
      <xs:sequence minOccurs="1">
        <xs:element name="Id" type="xs:integer" nillable="false" minOccurs="1" maxOccurs="1"  />
        <xs:element type="cmn:AddressType" name="ResidentialAddress" minOccurs="1" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>

  </xs:element>
</xs:schema>

Schema Address.xsd:

<xs:schema 
    targetNamespace="http://company.com/Common"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="AddressType">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Line1" minOccurs="1" maxOccurs="1" />
        <xs:element name="Line2" minOccurs="0" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
+1  A: 

It's because you actually declared AddressType as an element, not a type. If you want to use it as an element in your main schema, use:

<xs:element ref="cmn:AddressType" minOccurs="1" maxOccurs="1" />

If you want it to be a type, then get rid of the element declaration in your address schema and put in <xs:complexType name="AddressType"> instead.

xcut