Question

I would like to have an array of files all in the same namespace for simplicity sake and because they are conceptually related. I have a main or central xsd that will include other schema files and basically serve as a global root element. My problem is best illustrated by example, but I basically cannot get my non-central schema to validate and it's a namespace issue:

Schema 1 (supporting):

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

    <xsd:simpleType name="test">
        <xsd:restriction base="xsd:string">
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:complexType name="PersonType">
        <xsd:sequence>
            <xsd:element name="Name" type="test" />
            <xsd:element name="SSN" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

Schema 2 (central):

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.company.org"
            xmlns="http://www.company.org"
            elementFormDefault="qualified">

    <xsd:include schemaLocation="http://www.person.org"/>

    <xsd:element name="Company">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Person" type="PersonType"
                             maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Schema 2 is fine, schema 1 does not validate. "test" has no namespace, and I don't know how to give it one without destroying my intent to use 1 namespace for all my files.

Was it helpful?

Solution

It's not clear exactly what question you're asking, so I'll guess the question is "why does schema document 1 not validate, when schema document 2 does validate?"

I can't answer that, because I'm unable to reproduce your results. Both of your schema documents raise errors in the form in which you provide them.

Schema document 1 refers, in the definition of element (http://www.company.org, Name), local to complex type (http://www.company.org, PersonType), to a type named (http://www.person.org, test). But the namespace http://www.person.org has not been imported, so references to components in that namespace are not legal.

The specification type="test" is interpreted as reference to (http://www.person.org, test) because when "test" is interpreted as a QName, its namespace name is taken to be the default namespace, if there is one. Here, the default namespace (declared on the xsd:schema element) is http://www.person.org.

If -- this is sheer speculation on my part -- you want to refer to the type whose name is (http://www.company.org, test), which is declared on lines 7-10 of schema document 1, then you need to bind a namespace prefix to namespace http://www.company.org and use that prefix. It would work, for example, to change the declaration of Name to

<xsd:element name="Name" type="tns:test" 
             xmlns:tns="http://www.company.org"/>

or (using the default namespace, in order to avoid having to think of a prefix):

<xsd:element name="Name" type="test" 
             xmlns="http://www.company.org"/>

Note that the simple type declared on lines 7-10 has the expanded name (http://www.company.org, test) -- I don't know what you mean by saying "'test' has no namespace", but you may want to check your assumptions.

Schema document 2 raises an error because the schema location you specify on the xsd:include on line 6 produces, when dereferenced, a document which is not an XSD schema document (it's an HTML page).

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top