XML 模式验证:cvc-complex-type.2.4.a

发布于 2024-12-05 15:02:03 字数 1009 浏览 0 评论 0原文

我正在尝试根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汐鸠 2024-12-12 15:02:03

您的架构将“品牌”定义为不在命名空间中。这就是 '{"":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 the http://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 attribute elementFormDefault="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.

孤独患者 2024-12-12 15:02:03

您尚未验证 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>

You have not validated the attribute within cars, which is the url of the namespace, this should work:

<?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>
无所的.畏惧 2024-12-12 15:02:03

尝试使用此 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

Try with this 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>

Your elementFormDefault in the schema is "unqualified", see: https://www.xfront.com/HideVersusExpose.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文