XML 模式验证:cvc-complex-type.2.4.a
我正在尝试根据 XML 架构验证我的 XML 文档。
这是我的架构:
<?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>
这是我的 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>
现在,当我验证文档(通过 Eclipse)时,我在第 4 行收到以下消息:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'brand'. One of '{"":brand}' is expected.
此消息没有任何意义:(。而且这非常困难(不可能? )到谷歌解决方案,
谢谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的架构将“品牌”定义为不在命名空间中。这就是
'{"":brand}'
的含义。但在您的 XML 文档中,“brand”元素位于http://cars.example.org/
命名空间中。因此它们不匹配,您会收到验证错误。要将架构中的“brand”元素声明为位于
http://cars.example.org/
命名空间中,请将属性elementFormDefault="qualified"
添加到模式元素。我建议为了完整起见,您还将
attributeFormDefault="unqualified"
添加到 schema 元素,尽管在本例中这不是您的问题。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 thehttp://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 attributeelementFormDefault="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.您尚未验证 cars 中的属性,即命名空间的 url,这应该有效:
You have not validated the attribute within cars, which is the url of the namespace, this should work:
尝试使用此 XML:
架构中的 elementFormDefault 是“不合格的”,请参阅: https://www.xfront .com/HideVersusExpose.html
Try with this XML:
Your elementFormDefault in the schema is "unqualified", see: https://www.xfront.com/HideVersusExpose.html