Xml – Is this a bug in XSD implementation? Why doesn’t this work

xmlxsd

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:ExceptionType' 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">
    <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

Best Answer

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

Related Topic