验证 xml 和 xsd 时出错

Error while validation xml and xsd

我有这个简单的xml

<?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>a@a.ch</email>
        </person>
        <person id="2">
            <name>B</name>
            <kuerzel>b</kuerzel>
            <email>b@b.ch</email>
        </person>
        <person id="3">
            <name>C</name>
            <kuerzel>c</kuerzel>
            <email>c@c.ch</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- 文件有什么问题?我似乎找不到错误:(

提前致谢

您必须指定 person 元素可以出现多次

像这样扩展你的 xsd 人:

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

使用模式,我们可以使用 maxOccurs 和 minOccurs 属性定义元素可能出现的次数。 maxOccurs 指定元素的最大出现次数,minOccurs 指定元素的最小出现次数。 maxOccurs 和 minOccurs 的默认值都是 1!

问题是你没有定义最大发生次数你可以这样解决问题

<?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>