Reputation: 35
Hello I'm totally new in XML / XSD. Does anyone can help me out with this: XML (given):
<?xml version="1.0" encoding="UTF-8"?>
<filmliste xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="filmliste.xsd"
author="Max" datum="20.01.2016">
<film>
<titel> Movie</titel>
<regiseur> abc </regiseur>
<erscheinungsjahr> 2015 </erscheinungsjahr>
<schauspieler> abc </schauspieler>
<bewertung> abc </bewertung>
</film>
<film>
<titel> Movie2 </titel>
<erscheinungsjahr> 2015 </erscheinungsjahr>
<regiseur> abc </regiseur>
<schauspieler> abc </schauspieler>
<genre> abc </genre>
</film>
</filmliste>
My schema :
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="filmliste">
<xs:complexType>
<xs:sequence>
<xs:element name="film">
<xs:complexType>
<xs:choice >
<xs:element type="xs:string" name="titel" maxOccurs="1"/>
<xs:element type="xs:string" name="regiseur" maxOccurs="unbounded"/>
<xs:element type="xs:float" name="erscheinungsjahr" maxOccurs="1"/>
<xs:element type="xs:string" name="schauspieler" maxOccurs="unbounded"/>
<xs:element type="xs:float" name="bewertung" minOccurs="0"/>
<xs:element type="xs:string" name="genre"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="author"/>
<xs:attribute type="xs:string" name="datum"/>
</xs:complexType>
</xs:element>
</xs:schema>
Now i have two validation errors in my XML document. 1: Element 'regiseur': This element is not expected. 2: Element 'film': This element is not expected.
Upvotes: 2
Views: 66
Reputation: 116528
The xsd:choice
element is incorrect. It allows only one of the contained members to appear. That is, you can have a titel
OR a regiseur
but not both. Use either xsd:all
(items appear in any order) or xsd:sequence
(items appear in the specified order) instead. Judging by your XML, the order is not important so this should be xsd:all
.
The default of xsd:sequence@maxOccurs
is 1
. Therefore it is complaining at the second instance of film
. Specify an appropriate maximum bound:
<xs:sequence maxOccurs="unbounded">
Upvotes: 1
Reputation: 98
The error is that you are using the <xs:choice>
tag.
Using this tag means that you can only put in only one of the choices (child elements). Change it to either one of below tags.
<xsd:all>
- the child elements can appear in any order.
<xsd:sequence>
- the child elements can only appear in the order mentioned.
Upvotes: 1