2015-08-25 125 views
0

我有這個簡單的XML錯誤而驗證XML和XSD

<?xml version="1.0" encoding="UTF-8"?> 
<school xmlns="http://www.w3schools.com" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.w3schools.com Projekt.xsd"> 
    <personen> 
     <person id="1"> 
      <name>A</name> 
      <kuerzel>a</kuerzel> 
      <email>[email protected]</email> 
     </person> 
     <person id="2"> 
      <name>B</name> 
      <kuerzel>b</kuerzel> 
      <email>[email protected]</email> 
     </person> 
     <person id="3"> 
      <name>C</name> 
      <kuerzel>c</kuerzel> 
      <email>[email protected]</email> 
     </person> 
    </personen> 
</school> 

,並定義了以下XSD

<?xml version="1.0"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.w3schools.com" 
xmlns="http://www.w3schools.com" 
elementFormDefault="qualified"> 

<xs:element name="school"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element name="personen"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element name="person"> 
         <xs:complexType> 
          <xs:sequence>   
           <xs:element name="name" type="xs:string"/> 
           <xs:element name="kuerzel" type="xs:string"/> 
           <xs:element name="email" type="xs:string"/> 
          </xs:sequence> 
          <xs:attribute name="id" type="xs:integer" /> 
         </xs:complexType> 
        </xs:element> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 
</xs:schema> 

當我在網上驗證工具驗證這兩個文件,我得到以下錯誤:

Cvc-complex-type.2.4.d: Invalid Content Was Found Starting With Element 'person'. No Child Element Is Expected At This Point.. Line '10', Column '18'.

爲什麼我得到這個錯誤? 我的xsd-File有什麼問題?我似乎無法找到錯誤:(

在此先感謝

回答

2

的問題是事實,你沒有定義maxOccurs的就可以解決這個問題這樣

<?xml version="1.0"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.w3schools.com" 
xmlns="http://www.w3schools.com" 
elementFormDefault="qualified"> 

<xs:element name="school"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element name="personen" maxOccurs="unbounded"> 
      <xs:complexType> 
       <xs:choice minOccurs="0" maxOccurs="unbounded"> 
        <xs:sequence> 
        <xs:element name="person"> 
         <xs:complexType> 
          <xs:sequence>   
           <xs:element name="name" type="xs:string"/> 
           <xs:element name="kuerzel" type="xs:string"/> 
           <xs:element name="email" type="xs:string"/> 
          </xs:sequence> 
          <xs:attribute name="id" type="xs:integer" /> 
         </xs:complexType> 
        </xs:element> 
       </xs:sequence> 
       </xs:choice> 
      </xs:complexType> 

     </xs:element> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 
</xs:schema> 
1

必須specifiy,該人元素可出現不止一次

擴展您的XSD的人是這樣的:

<xs:element name="person" maxOccurs="unbounded"> 

使用模式,我們可以使用maxOccurs和minOccurs屬性定義元素的可能出現次數maxOccurs指定元素出現次數的最大值,minOccurs指定元素出現次數的最小值。 maxOccurs和minOccurs的默認值是1!