【问题标题】:XML schema validation: cvc-complex-type.2.4.aXML 模式验证:cvc-complex-type.2.4.a
【发布时间】:2011-11-20 22:49:56
【问题描述】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      要将架构中的“品牌”元素声明为位于 http://cars.example.org/ 命名空间中,请将属性 elementFormDefault="qualified" 添加到架构元素中。

      为了完整起见,我建议您也将 attributeFormDefault="unqualified" 添加到架构元素中,尽管在这种情况下这不是您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 2014-02-04
        • 2018-08-24
        • 1970-01-01
        相关资源
        最近更新 更多