2015-08-25 91 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!