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.
id
andname
are unique and required. There cannot be more than one element from those.arg
can 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.