I am trying to restrict elements inside a choice element with minOccurs and maxOccurs, but it doesn't look like it is working.
I created a choice and tried to restrict "person" to 1 occurence max, and "address" to 1 occurence min, but when I try to validate a XML file containing 2 occurences of "person" and 0 occurence of "address", the validator I use (Xerces) says it is valid.
Is what I am trying to do correct? Is there any way to force the occurence of an element inside a choice?
Here are my XSD and XML :
mySchema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="family">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="person" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="address" type="xs:string" minOccurs="1"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
instance.xml
<?xml version="1.0" encoding="UTF-8"?>
<family>
<person>
<name> Kurtis </name>
<firstname> John </firstname>
<age> 35 </age>
</person>
<person>
<name> Kurtis </name>
<firstname> Helena </firstname>
<age> 33 </age>
</person>
</family>
Actually
<xs:element name="person" maxOccurs="1">should be unnecessary and<xs:element name="person">should be enougn.<xs:choice maxOccurs="unbounded">is reason why two person are evaluated as ok, just try<xs:choice>.In
<xs:element name="address" type="xs:string" minOccurs="1"/>you should add maxOccurs attribute<xs:element name="address" type="xs:string" minOccurs="1" maxOccurs="unbounded" />Edit:
You could make something like this:
In this case xml with one person will validate
or xml with many address elements will validate
XML with two persons won't validate as well as XML with one person and some address elements (because of choice structure).
If you needed have both person and address elements in one XML you should change choice into sequence like following
so e.g. following xml will validate
as well as
I previous example there could be only 0 or 1 "person" element and it had to be first of all elements since sequence enforce this order.
If you needed address elements could precede person element you would need change the model to all. But unfortunately at this model you cannot have more occurences of one elements so you should "wrap" address elements into another one like
In this case following XMLs will validate
Example 1
Example 2
Example 3