I’m trying to construct a xsd scheme to validate a xml.
The xml is:
<payments>
<pay3>5.1</pay3>
<pay1>1</pay1>
<pay2>50</pay2>
<pay3>2</pay3>
</payments>
Tags <pay2>, <pay3> and <pay2> are optional and <pay1> is mandatory. All the <payX> tags may occur in any order and more than once or not to occur (except for <pay1>).
So far I made the following xsd types but it is not working correctly if <pay1> is not present:
<xs:simpleType name="TPayment">
<xs:restriction base="xs:decimal">
<xs:pattern value="[+]?\d+(\.\d{2})?" />
<xs:minInclusive value="0" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="TECR_Payments">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="pay1" type="TPayment" maxOccurs="unbounded" />
<xs:element name="pay2" type="TPayment" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="pay3" type="TPayment" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="pay4" type="TPayment" minOccurs="0" maxOccurs="unbounded" />
</xs:choice>
</xs:complexType>
How to set that <pay1> is mandatory?
Use this for your
TECR_Paymentstype:This allows any number (including zero) of
<pay2>,<pay3>, or<pay4>elements in any order, then requires one<pay1>, and then allows any number of<payX>elements in any order.Note that specifying
minOccursormaxOccurson anxs:elementinside anxs:choicedoesn’t really have the effect you want: The choice is first made between the type of element, and then the choice is made about how many elements of that type to use.