xeraphim
xeraphim

Reputation: 4645

Error while validation xml and xsd

I have this simple 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>[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>

and defined the following 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>

When I validate these two files in an online validation tool, I get the following error:

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'.

Why am I getting this error? What is wrong in my xsd-File? I can't seem to find the error :(

Thanks in advance

Upvotes: 0

Views: 85

Answers (2)

othman.Da
othman.Da

Reputation: 631

the problem is in the fact that you do not define a maxoccurs you can solve the problem this way

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

Upvotes: 2

Xavjer
Xavjer

Reputation: 9264

You must specifiy, that the person element can occur more than once

Extend your xsd person like this:

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

With schemas we can define the number of possible occurrences for an element with the maxOccurs and minOccurs attributes. maxOccurs specifies the maximum number of occurrences for an element and minOccurs specifies the minimum number of occurrences for an element. The default value for both maxOccurs and minOccurs is 1!

Upvotes: 1

Related Questions