Search code examples
xmlschema

XML schema validation: cvc-complex-type.2.4.a


I'm trying to validate my XML document against my XML schema.

This is my schema:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://cars.example.org/">
  <element name="cars">
    <complexType>
      <sequence minOccurs="0" maxOccurs="unbounded">
        <element name="brand" type="string"/>
      </sequence>
    </complexType>
  </element>
</schema>

and this is my XML document:

<?xml version="1.0" encoding="UTF-8"?>
<cars xmlns="http://cars.example.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://cars.example.org/ cars.xsd">
  <brand>x</brand>
</cars>

Now when I'm validating the document (via Eclipse) I get following message on line 4:

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

This message doesn't make any sense :(. And it's very hard (impossible?) to google solution.

Thank you for your help.


Solution

  • Your schema is defining "brand" as being in no namespace. That's what '{"":brand}' means. But in your XML document the "brand" element is in the http://cars.example.org/ namespace. So they don't match and you get your validation error.

    To declare the "brand" element in your schema as being in the http://cars.example.org/ namespace, add the attribute elementFormDefault="qualified" to the schema element.

    I suggest that for completeness you also add attributeFormDefault="unqualified" to the schema element, although that is not your problem in this case.