XML 架构错误 "Element not allowed for content model"

XML Schema error "Element not allowed for content model"

我对 XML 和 XSD 还很陌生,需要一些帮助。我不断收到此验证错误“内容模型不允许元素 header (header+, objective*,pubs?)。它出现在 [=23= 的最后一行] 我不知道是什么原因造成的。这是 XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema"  elementFormDefault="qualified">

  <xs:element name="resume">
    <xs:complexType>
     <xs:sequence>
        <xs:element name="header" minOccurs="1" maxOccurs="unbounded"  >
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="NameType" />
              <xs:element name="contact" type="ContactType" />
            </xs:sequence>
          </xs:complexType>  
        </xs:element> 
        <xs:element name="objective" type="ObjectiveType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="pubs" minOccurs="0" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="pub" type="PubType"  minOccurs="0" maxOccurs="unbounded"/>                  
            </xs:sequence>                
          </xs:complexType>           
        </xs:element>
      </xs:sequence>  
    </xs:complexType>
  </xs:element>      


   <xs:complexType name="NameType">
    <xs:group ref="NameGroup" />
  </xs:complexType>

   <xs:group name="NameGroup">
     <xs:sequence>
       <xs:element name="firstname" type="xs:string" />
       <xs:element name="middlenames" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
       <xs:element name="surname" type="xs:string" />
     </xs:sequence>   
   </xs:group>

   <xs:complexType name="ContactType">
    <xs:sequence>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="phone" type="xs:string" />
        <xs:element name="website" type="xs:string" />
        <xs:element name="email" type="xs:string" />
        <xs:element name="address" type="xs:string" />
      </xs:choice>  
   </xs:sequence>
  </xs:complexType>

    <xs:complexType name="ObjectiveType">
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="para" type="xs:string" /> 
      </xs:sequence>    
    </xs:complexType>

    <xs:complexType name="PubType">
      <xs:sequence >
        <xs:element name="bookTitle" type="xs:string"  />
        <xs:element name="date"  minOccurs="0" maxOccurs="1" >
          <xs:complexType>
            <xs:sequence>
              <xs:element name="month" type="xs:string" />
              <xs:element name="year" type="xs:string" />
            </xs:sequence>  
          </xs:complexType>
          </xs:element>
        <xs:element name="publisher" type="xs:string" />
        <xs:element name="url" type="xs:string"  minOccurs="0" maxOccurs="1" />
       </xs:sequence>    
    </xs:complexType>


</xs:schema>

这里是 XML 代码:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resume xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="A2_Schema_task2.xsd">

  <header>
    <name>
     <firstname>John</firstname>
     <middlenames>E.</middlenames>
     <surname>Doe</surname>
    </name>
    <contact>
     <email>jdoe@mycompany.net</email>
    </contact>
  </header>

  <objective>
    <para>Seeking a position in publishing as Associate Manager</para>
    <para>The position will allow me to utilize my experiences to mentor and train my team. </para>
  </objective>

  <pubs>
    <pub>
      <bookTitle>Just XML</bookTitle>
      <publisher>Wiley</publisher>
      <url>http://mycompany/book1</url>
    </pub>

    <pub>
      <bookTitle>XPath and XPointer</bookTitle>
      <date><month>August</month><year>2002</year></date>
      <publisher>O'Reilly and Associates</publisher>
      <url>http://mycompany/book1</url>
    </pub>
  </pubs>

  <header>
    <name>
     <firstname>Maya</firstname>
     <surname>Wells</surname>
    </name>
    <contact>
     <phone>416-785-2598</phone>
    </contact>
  </header>

  <objective>
    <para>Seeking a position in publishing as Associate Manager</para>
  </objective>

  <pubs>
    <pub>
      <bookTitle>Just JavaScript</bookTitle>
      <publisher>Wrox</publisher>
    </pub>
  </pubs>

  <header>
    <name>
     <firstname>Irene</firstname>
     <surname>Mathieu</surname>
    </name>
    <contact>
     <phone>905-785-2598</phone>
     <website>www.irenemathieu.com</website>
     <email>irenemathieu@gmail.com</email>
     <address>105 Bathurst Street , Toronto</address>
    </contact>
  </header>

  <objective>
    <para>Seeking a position in publishing as Publishing Manager with your company</para>
   <para>Seasoned manager offering 30 years of progressive experience as a Publishing Manager,  willing to provide advice and leadership for educational publishing in its broadest form.</para>
  </objective>

</resume>

感谢您的帮助!

在您的 XSD 文件中,您首先定义了 1..* header,然后是 0..* objective,最后是 0..1 酒吧。所以你的文件应该是这样的:

<resume>
   <header>...</header>
   <header>...</header>
   <header>...</header>

   <objective>...</objective>
   <objective>...</objective>
   <objective>...</objective>
   <objective>...</objective>
   <pubs>...</pubs>

</resume>

在你的 XML 文件中你有

<resume>
   <header>...</header>
   <objective>...</objective>
   <pubs>...</pubs>

   <header>...</header>
   <objective>...</objective>
   <pubs>...</pubs>

   <header>...</header>
   <objective>...</objective>
</resume>

所以您的 XML 文件没有验证您的 XSD 文件是正常的

您的xml无效。你的 xsd 说:

  • header元素应该是1个或者多个一个接一个
  • 在 header 之后,你应该有 objective 这是可选的
  • 在 objective 或 header 之后,您应该有再次可选的酒吧,应该出现一次或 none。

在您的 xml 中,您有多个酒吧及其结构:

<header ..>
<objective ...>
<pubs ..>
<header ..>
<objective ...>
<pubs ..>
<header ..>
<objective ...>

谢谢大家的帮助。我发现我需要添加一个新元素 (person),其中包含 header、objective、pubs 元素,每个新人都可以重复使用这些元素。这是我为修复 xml 和 xsd:

所做的示例

xml:

  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <resume xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="A2_Schema_task2.xsd">
<person>
  <header>
    <name>
     <firstname>John</firstname>
     <middlenames>E.</middlenames>
     <surname>Doe</surname>
    </name>
    <contact>
     <email>jdoe@mycompany.net</email>
    </contact>
  </header>

  <objective>
   <para>Seeking a position in publishing as Associate Manager</para>
    <para>The position will allow me to utilize my experiences to mentor and train my team. </para>
  </objective>

  <pubs>
    <pub>
      <bookTitle>Just XML</bookTitle>
      <publisher>Wiley</publisher>
      <url>http://mycompany/book1</url>
    </pub>

    <pub>
      <bookTitle>XPath and XPointer</bookTitle>
      <date><month>August</month><year>2002</year></date>
      <publisher>O'Reilly and Associates</publisher>
      <url>http://mycompany/book1</url>
    </pub>
  </pubs>
</person>

和XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema"  elementFormDefault="qualified">

  <xs:element name="resume">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="person" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="header"  >
                <xs:complexType>
                 <xs:sequence>
                   <xs:element name="name" type="NameType" />
                   <xs:element name="contact" type="ContactType" />
                 </xs:sequence>
               </xs:complexType>  
              </xs:element> 
              <xs:element name="objective" type="ObjectiveType" minOccurs="0" maxOccurs="unbounded"/>
              <xs:element name="pubs" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="pub" type="PubType"  minOccurs="0" maxOccurs="unbounded"/>                  
                  </xs:sequence>                
                </xs:complexType>           
              </xs:element>
             </xs:sequence>
          </xs:complexType>  
         </xs:element> 
      </xs:sequence>  
    </xs:complexType>
  </xs:element>      

现在一切正常并得到验证。谢谢大家的帮助!