代码之家  ›  专栏  ›  技术社区  ›  xeraphim

验证xml和xsd时出错

  •  0
  • xeraphim  · 技术社区  · 9 年前

    我有一个简单的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复杂类型。2.4.d:发现以开头的无效内容 元素“person”。此点不需要子元素..行 “10”列,“18”列。

    为什么我会出现这个错误? 我的xsd文件中有什么问题?我似乎找不到错误:(

    提前感谢

    2 回复  |  直到 9 年前
        1
  •  2
  •   othman.Da    9 年前

    问题在于你没有定义maxoccurrence,你可以这样解决问题

    <?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>
    
        2
  •  1
  •   Xavjer    9 年前

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

    这样扩展您的xsd人员:

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

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