Why can an element in an included no-namespace schema reference a type in the including schema when there is no default namespace declaration?

Hi Folks,

The following schema document is erroneous because the element A1 is trying to reference a type XYZ in no-namespace:

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

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

    <xs:simpleType name="XYZ">
        <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
        </xs:restriction>
    </xs:simpleType>
    
    <xs:element name="A1" type="XYZ" />
    
</xs:schema>
----------------------------------------------------------------------

The error is fixed by adding a default namespace declaration:

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

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

    <xs:simpleType name="XYZ">
        <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
        </xs:restriction>
    </xs:simpleType>
    
    <xs:element name="A1" type="XYZ" />
    
</xs:schema>
----------------------------------------------------------------------

Good.

Now let's remove the element A1 and remove the default namespace declaration. This is a valid schema:

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

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

    <xs:simpleType name="XYZ">
        <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
        </xs:restriction>
    </xs:simpleType>
    
</xs:schema>
----------------------------------------------------------------------

Notice that the schema document includes B.xsd. Let's look at it. It is a no-namespace schema document:

B.xsd
----------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    
    <xs:element name="B2" type="XYZ" />
    
</xs:schema>
----------------------------------------------------------------------

Since A.xsd has a targetNamespace the B.xsd schema document is namespace-coerced to the namespace of A.xsd.

Observe that element B2 references type XYZ which is in A.xsd.

When I validate A.xsd it validates fine.

Hey!  How can that be?

Recall that I removed the default namespace declaration from A.xsd. 

Why can element B2 reference type XYZ without a default namespace declaration but element A1 couldn't?

/Roger

Received on Saturday, 28 January 2012 23:01:00 UTC