我正在尝试根据我的 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.
此消息没有任何意义 :(。而且很难(不可能?)谷歌解决方案。
感谢您的帮助。
最佳答案
您的架构将“品牌”定义为不在命名空间中。这就是 '{"":brand}'
的意思。但是在您的 XML 文档中,“品牌”元素位于 http://cars.example.org/
命名空间中。因此它们不匹配,您会收到验证错误。
要将架构中的“品牌”元素声明为位于 http://cars.example.org/
命名空间中,请添加属性 elementFormDefault="qualified"
到架构元素。
为了完整起见,我建议您也将 attributeFormDefault="unqualified"
添加到架构元素中,尽管在这种情况下这不是您的问题。
关于XML 架构验证 : cvc-complex-type. 2.4.a,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7483331/