XML 架构验证:cvc-complex-type.2.4.a

问题描述 投票:0回答:3

我正在尝试根据我的 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.

这条消息没有任何意义:(。而且很难(不可能?)谷歌解决方案。

谢谢您的帮助。

xml schema
3个回答
16
投票

您的架构将“品牌”定义为不位于任何名称空间中。这就是

'{"":brand}'
的意思。但在您的 XML 文档中,“brand”元素位于
http://cars.example.org/
命名空间中。因此它们不匹配,您会收到验证错误。

要将架构中的“brand”元素声明为位于

http://cars.example.org/
命名空间中,请将属性
elementFormDefault="qualified"
添加到架构元素。

我建议为了完整起见,您还可以将

attributeFormDefault="unqualified"
添加到 schema 元素,尽管在本例中这不是您的问题。


0
投票

您尚未验证 cars 中的属性,即命名空间的 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>

0
投票

尝试使用此 XML:

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

架构中的 elementFormDefault 是“不合格的”,请参阅:https://www.xfront.com/HideVersusExpose.html

© www.soinside.com 2019 - 2024. All rights reserved.