开发者

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开发者_StackOverflow any sense :(. And it's very hard (impossible?) to google solution.

Thank you for your help.


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.


You have not validated the attribute within cars, which is the url of the namespace, this should work:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜