加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 为什么XSD验证期望{element}使用大括号而不仅仅是元素?

发布时间:2020-12-16 07:58:31 所属栏目:百科 来源:网络整理
导读:当我尝试针对以下XSD验证以下XML时,我收到以下错误: cvc-complex-type.2.4.a: Invalid content was found starting with element ’personal’. One of ‘{personal} expected. XML main xmlns = "http://www.example.com" xmlns:xsi = "https://www.w3.org
当我尝试针对以下XSD验证以下XML时,我收到以下错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element >’personal’. One of ‘{personal} expected.

XML

<main xmlns = "http://www.example.com"
      xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation = "main.xsd">
  <personal>
    <full-name>John Smith</full-name>
    <contact>
      <street-address>12345 Example Street</street-address>
      <city>Somewhere</city>
      <state>EX</state>
      <postal-code>111 111</postal-code>
      <phone>123 456 7890</phone>
      <email>myemail@example.com</email>
    </contact>
  </personal>
</main>

XSD

<xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
            targetNamespace = "http://www.example.com"
            xmlns = "http://www.example.com">
  <xsd:element name = "main" type = "main-type"/>

  <xsd:complexType name = "main-type">
    <xsd:all>
      <xsd:element name = "personal" type = "personal-type"/>
    </xsd:all>
  </xsd:complexType>

  <xsd:complexType name = "personal-type">
    <xsd:all>
      <xsd:element name = "full-name" type = "xsd:string" 
                   minOccurs = "1"/>
      <xsd:element name = "contact" type = "contact-type" 
                   minOccurs = "1"/>
    </xsd:all>
  </xsd:complexType>

  <!--Different xsd:strings for contact information in contact-type-->
  <xsd:complexType name = "contact-type">
    <xsd:all>
      <xsd:element name = "street-address" type = "xsd:string"/>
      <xsd:element name = "city" type = "xsd:string"/>
      <xsd:element name = "state" type = "xsd:string"/>
      <xsd:element name = "postal-code" type = "xsd:string"/>
      <xsd:element name = "phone" type = "xsd:string"/>
      <xsd:element name = "email" type = "xsd:string"/>
    </xsd:all>
  </xsd:complexType>
</xsd:schema>

有什么问题,我该如何解决?

发布的XML在您发布的错误消息之前有两个初步问题:

>改变

xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance"

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

>改变

xsi:schemaLocation = "main.xsd"

xsi:schemaLocation = "http://www.example.com main.xsd"

现在,您发布的XML和XSD实际上将处于显示已发布问题的状态:

[Error] main.xml:4:13: cvc-complex-type.2.4.a: Invalid content was
found starting with element ‘personal’. One of ‘{personal}’ is
expected.

说明:此错误告诉您,根据您的XSD,个人应该没有名称空间;预期“{personal}”之一中的{和}表示此.

您可能认为,因为您的XSD声明targetNamespace =“http://www.example.com”,因此其所有组件都放入http://www.example.com命名空间.对于本地声明的组件,情况并非如此,但除非您设置elementFormDefault =“qualified” – 否则默认值是不合格的.

默认情况下,本地声明的元素不在命名空间中

因此,进行最后一次更改:添加

elementFormDefault="qualified"

到xsd:schema元素,然后你的XML对你的XSD有效.

另见this answer about what elementFormDefault means.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读