XML 模式验证:cvc-complex-type.2.4.a
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7483331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML schema validation: cvc-complex-type.2.4.a
提问by woky
I'm trying to validate my XML document against my XML schema.
我正在尝试根据我的 XML 模式验证我的 XML 文档。
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 文档:
<?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:
现在,当我验证文档(通过 Eclipse)时,我在第 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.
感谢您的帮助。
回答by Alohci
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.
您的架构将“品牌”定义为没有命名空间。就是这个'{"":brand}'意思。但是在您的 XML 文档中,“brand”元素位于http://cars.example.org/命名空间中。所以它们不匹配,你会得到验证错误。
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.
要将架构中的“brand”元素声明为在http://cars.example.org/命名空间中,请将属性添加elementFormDefault="qualified"到架构元素。
I suggest that for completeness you also add attributeFormDefault="unqualified"to the schema element, although that is not your problem in this case.
我建议为了完整性起见,您还添加attributeFormDefault="unqualified"到 schema 元素,尽管在这种情况下这不是您的问题。
回答by Mansuro
You have not validated the attribute within cars, which is the url of the namespace, this should work:
您尚未验证汽车中的属性,即命名空间的 url,这应该有效:
<?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>

