Eleno
Eleno

Reputation: 3016

Possible namespace problem during XML validation

The following XML with XSD causes the validation error:

The element 'choices' in namespace 'http://www.test.it' has invalid child element 'choice' in namespace 'http://www.test.it'. List of possible elements expected: 'choice'.

This is choices.xml:

<?xml version="1.0" encoding="utf-8"?>
<choices xmlns="http://www.test.it"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
         xsd:schemaLocation="http://www.test.it ./schema/choices.xsd">
    <choice>yes</choice>
</choices>

This is schema/choices.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema  xmlns="http://www.test.it"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.test.it">

    <xs:simpleType name="yes_or_no_t">
        <xs:restriction base="xs:string">
            <xs:enumeration value="yes" />
            <xs:enumeration value="no" />
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="choices" >
        <xs:complexType>
            <xs:all>
                <xs:element name="choice" type="yes_or_no_t" />
            </xs:all>
        </xs:complexType>
    </xs:element>

</xs:schema>

I must keep the xmlns="http://www.test.it" in the XML. The XML and XSD are meant to be local files (not published via network). I would rather keep the XSDs in a schema subdirectory.

Upvotes: 2

Views: 903

Answers (1)

kjhughes
kjhughes

Reputation: 111491

There were two problems...

Finding the XSD

Use xsi:schemaLocation for namespaced XML such as yours, not xsi:noNamespaceSchemaLocation.

See also:

Qualified elements

Add elementFormDefault="qualified" to the schema element of your XSD.

See also:

Upvotes: 1

Related Questions