tags:

views:

90

answers:

2

Hi,

I have one xsd file I'd rather not modify (Exceptions.xsd):

<xs:schema 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns="http://me.com/Exceptions.xsd" 
     targetNamespace="http://me.com/Exceptions.xsd" 
     elementFormDefault="qualified" 
     attributeFormDefault="unqualified"
>
    <xs:element name="Exception" type="ExceptionType" />

    <xs:complexType name="ExceptionType">
     <xs:sequence>
      <xs:element name="Code" type="xs:string" minOccurs="0"/>
      <xs:element name="Message" type="xs:string"/>
      <xs:element name="TimeStamp" type="xs:dateTime"/>
     </xs:sequence>
    </xs:complexType>
</xs:schema>

I want to create a new element with an other name that implements that ExceptionType (ExceptionsExtensions.xsd - Alternative 1).

<xs:schema 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns="http://me.com/Exceptions.xsd" 
     targetNamespace="http://me.com/Exceptions.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation=
       " 
       http://me.com/Exceptions.xsd Exceptions.xsd
       "
     elementFormDefault="qualified" 
     attributeFormDefault="unqualified">

    <xs:element name="SpecificException" type="ExceptionType" />
</xs:schema>

I get the error message: Type 'http://me.com/Exceptions.xsd%3AExceptionType' is not declared.

However, if I would do this (ExceptionExtensions.xsd - Alternative 2):

<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://me.com/Exceptions.xsd" 
    targetNamespace="http://me.com/Exceptions.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=
            " 
            http://me.com/Exceptions.xsd Exceptions.xsd
            "
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified">

    <xs:element name="SpecificException">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="innerException">
        <xs:complexType>
         <xs:sequence>
          <xs:any namespace="http://me.com/Exceptions.xsd" />       
         </xs:sequence>
        </xs:complexType>
       </xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>

I can validate

<?xml version="1.0" encoding="utf-8"?>
<SpecificException xmlns="http://me.com/Exceptions.xsd"&gt;
    <innerException>
     <Exception>
      <Code>12</Code>
      <Message>Message</Message>
      <TimeStamp>2009-08-27T11:30:00</TimeStamp>
     </Exception>
    </innerException>
</SpecificException>

So in Alternative 1 it CANNOT find the ExceptionType which is declared in Exceptions.xsd, but in Alternative 2 it CAN find the Exception-element which is declared in Exceptions.xsd.

Why doesn't Alternative 1 work?

Kind regards, Guillaume Hanique

+3  A: 

In your "Alternative 1", you're referencing "ExceptionType" - but it's not declared anywhere in that file.

Just because two files share the same namespace doesn't mean File A can depends on stuff in File B - you will need to connect the two!

Add an <xsd:include> to your second file:

<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://me.com/Exceptions.xsd" 
    targetNamespace="http://me.com/Exceptions.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://me.com/Exceptions.xsd Exceptions.xsd"
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified">

  <xs:include schemaLocation="exceptiontype.xsd"/>

  <xs:element name="SpecificException" type="ExceptionType" />
</xs:schema>

That should do the trick!

Marc

marc_s
That's it, thanks!
Guillaume Hanique
A: 

Check this question.

http://stackoverflow.com/questions/332792/can-i-have-one-xml-schema-xsd-include-another-xml-schema/489385#489385

Here is another link which clearly explains the differences between xsd:include & xsd:import.

Hope this helps

sandeep