Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
825 views
in Technique[技术] by (71.8m points)

xml - Why does XSD validation expect {element} with braces rather than just element?

When I try to validate the below XML against the below XSD I get the following error:

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>[email protected]</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>

What's the problem and how can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your XML as posted has two preliminary problems ahead of the error message you've posted:

  1. Change

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

    to

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

    xsi:schemaLocation = "main.xsd"
    

    to

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

Now, your posted XML and XSD actually will be in a state to exhibit your posted problem:

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

Explanation: This error is telling you that personal is expected to be in no namespace according to your XSD; the { and } in One of '{personal}' is expected indicates this.

You might think that since your XSD declares targetNamespace="http://www.example.com" that all of its components are thus placed into the http://www.example.com namespace. This is not true of locally declared components, however unless you set elementFormDefault="qualified" -- the default is unqualified.

Locally declared elements are in no namespace by default

Therefore, make one last change: Add

elementFormDefault="qualified"

to the xsd:schema element, and then your XML valid against your XSD.

See also this answer about what elementFormDefault means.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...