Gavin Williams
Gavin Williams

Reputation: 462

How do I use inheritance (ie xs:extension) in my own xml schema?

I'm trying to make my own xml schema in Visual Studio. But I can't make any complex types such as a Rectangle type, nor inheritable types such as Shape. The schema.xsd follows and the errors I'm getting are :

  1. Namespace '' is not available to be referenced in this schema. [line 6 xs:extension base="Shape">

  2. Undefined complexType 'Shape' is used as a base for complex type extension [line 6 again ]

Content:

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

<xs:complexType name="Rectangle">
 <xs:complexContent>
   <xs:extension base="Shape">
     <xs:sequence>
      <xs:element minOccurs="0" name ="X" type="xs:int"/>
      <xs:element minOccurs="0" name ="Y" type="xs:int"/>
      <xs:element minOccurs="0" name ="Width" type="xs:int"/>
      <xs:element minOccurs="0" name ="Height" type="xs:int"/>
    </xs:sequence>
   </xs:extension>
 </xs:complexContent>

</xs:complexType>
<xs:complexType name="Shape">
  <xs:sequence>
    <xs:element name="Complexity" type="xs:string"/>
  </xs:sequence>  
</xs:complexType>

Upvotes: 3

Views: 2611

Answers (2)

weston
weston

Reputation: 54781

To resolve Namespace '' is not available to be referenced in this schema. You can set the default '' namespace without the need to change the rest of the file:

Add this xmlns="MySchema"

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

Upvotes: 3

Michael Kay
Michael Kay

Reputation: 163272

You need to bind a prefix to the target namespace:

xmlns:m="MySchema"

and use this in references to components defined within the schema

base="m:Shape"

Upvotes: 4

Related Questions