Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

XSD schema namespace issues -- cvc-complex-type.2.4.a

I am trying to read an XML file, but he keeps throwing this error:

  • cvc-complex-type.2.4.a: Invalid content was found starting with element 'contact'. One of '{contact}' is expected.

  • lineNumber: 2; columnNumber: 68; cvc-elt.1: Cannot find the declaration of element 'contacts'.]

I have scoured the web to find answers. If I remove my targetNamespace, then it does not complain about finding the elements, but it begins to complain about how it cannot find my custom types declared in the tns namespace.

I am validating it here: http://www.corefiling.com/opensource/schemaValidate.html

I am using JAXB to ensure that the document conforms to its schema.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    targetNamespace="http://www.example.com/ContactsSchema"
    xmlns:tns="http://www.example.com/ContactsSchema">

    <xs:element name="contacts" type="tns:Contacts" />

    <xs:complexType name="Contacts">
        <xs:sequence>
            <xs:element name="contact" type="tns:Contact" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" />
    </xs:complexType>

    <xs:complexType name="Contact">
        <xs:sequence>
            <xs:element name="id" type="xs:long"/>
            <xs:element name="firstName" type="xs:string" />
            <xs:element name="lastName" type="xs:string" />
            <xs:element name="firstName" type="xs:string" />
            <xs:element name="address" type="tns:Address" />
            <xs:element name="favorite" type="xs:boolean" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Address">
        <xs:sequence>
            <xs:element name="street" type="xs:string" />
            <xs:element name="city" type="xs:string" />
            <xs:element name="state" type="xs:string" />
            <xs:element name="zip" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contacts xmlns="http://www.oracle.com/ContactsSchema" name="Test">
    <contact id="1">
        <firstName>Herman</firstName>
        <lastName>Munster</lastName>
        <address>
            <street>1313 Mockingbird Lane</street>
            <city>Camelot</city>
            <state>NJ</state>
            <zip>15490</zip>
        </address>
        <favorite>true</favorite>
    </contact>
    <contact id="2">
        <firstName>Al</firstName>
        <lastName>Bundy</lastName>
        <address>
            <street>9764 Jeopardy Lane</street>
            <city>Chicago</city>
            <state>IL</state>
            <zip>60629</zip>
        </address>
        <favorite>false</favorite>
    </contact>
</contacts>

Upvotes: 2

Views: 1007

Answers (2)

Petru Gardea
Petru Gardea

Reputation: 21638

To @Michael's point, you have to put in agreement the targetNamespace of your XSD with that used by your sample XML. Let's assume that the XML is right, and the XSD is wrong... since there are more problems than just the namespace mismatch...

  • Your XSD describes the id as an element, while the sample XML shows an attribute.
  • Your XSD shows a firstName after lastName, which is most likely a typo; nonetheless, it needs to be removed to match the XML.

This is a fixed XSD that matches your XML:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    targetNamespace="http://www.oracle.com/ContactsSchema"
    xmlns:tns="http://www.oracle.com/ContactsSchema">

    <xs:element name="contacts" type="tns:Contacts" />

    <xs:complexType name="Contacts">
        <xs:sequence>
            <xs:element name="contact" type="tns:Contact" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" />
    </xs:complexType>

    <xs:complexType name="Contact">
        <xs:sequence>
            <xs:element name="firstName" type="xs:string" />
            <xs:element name="lastName" type="xs:string" />
            <xs:element name="address" type="tns:Address" />
            <xs:element name="favorite" type="xs:boolean" />
        </xs:sequence>
            <xs:attribute name="id" type="xs:long"/>
    </xs:complexType>

    <xs:complexType name="Address">
        <xs:sequence>
            <xs:element name="street" type="xs:string" />
            <xs:element name="city" type="xs:string" />
            <xs:element name="state" type="xs:string" />
            <xs:element name="zip" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Upvotes: 3

Michael Kay
Michael Kay

Reputation: 163322

Your schema is for namespace http://www.example.com/ContactsSchema, but your instance is in namespace http://www.oracle.com/ContactsSchema.

Upvotes: 1

Related Questions