I have an XML file to validate against XSD schema. This is how my XML file looks like.
<service>
<id>myid</id>
<name>myname</name>
<arg>arg1</arg>
<arg>arg2</arg>
</service>
These are the validation rules.
idandnameare unique and required. There cannot be more than one element from those.argcan be repeat- The order does not matter. Elements can be in any order.
This is the XSD file I created for this.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="service">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element type="xs:string" name="id" minOccurs="1" maxOccurs="1"/>
<xs:element type="xs:string" name="name" minOccurs="1" maxOccurs="1"/>
<xs:element type="xs:string" name="arg" minOccurs="1" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
When I try to validate with this (https://www.freeformatter.com/xml-validator-xsd.html) online validator this works. But it now shows any error even when there are two id elements or no id element.
<service>
<id>myid</id>
<id>anotherid</id>
<name>myname</name>
<arg>arg1</arg>
<arg>arg2</arg>
</service>
This should be wrong since there are two id elements. How to have both repeatable and not repeatable elements in choice elements. Sequence is not possible here since the order does not matter.
<service>
<name>myname</name>
<arg>arg1</arg>
<arg>arg2</arg>
</service>
This should fail since the id element is not there.
groupdoesn't I have to usechoice? - Buddhika Chathurangaxs:sequenceoption all elements must be in order. If you use thexs:choiceoption with themaxoccursrestriction it will create as much elements that the maxxoccurs is set on. thexs:alloption also gives you the freedom to have elements in a random order. but themaxoccursrestriction is not possible here. The solution could be a type that holds the unbounded arg element and assign an element to that resulting into a <args><arg></arg><arg></arg></args> structure. - martijn