XML Schema错误"元素不允许用于内容模型"

时间:2015-10-23 13:30:32

标签: xml xsd

我是XML和XSD的新手,需要一些帮助。我不断收到此验证错误"内容模型不允许使用Element标头(header +,objective *,pubs?)。它出现在XML的最后一行,我无法弄清楚可能导致它的原因。这是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>

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

在你的XSD文件中,你定义为第一个.. .. *标题,然后是0 .. *目标,最后是0..1个pubs。所以你的文件应该是这样的:

<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文件,这是正常的

答案 1 :(得分:0)

您的xml无效。你的xsd说:

  • 标题元素应该是1个或多个一个接一个
  • 在标题之后,你应该有一个可选的目标
  • 在目标或标题之后,你应该有一个pubs,它也是可选的,应该出现一次或没有。

在你的xml中,你有多个pubs及其结构:

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

答案 2 :(得分:0)

谢谢大家的帮助。我想我需要添加一个新元素(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>      

现在一切正常并有效。谢谢大家的帮助!