I have the following element in my XSD:
<xs:element name="documents" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="invoice" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="report" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="additional" minOccurs="0" maxOccurs="unbounded">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
You can see that documents must always have an invoice and optionally it can have a single report and zero or more additionals.
The problem is that these elements can have a different order of appearance, so I can´t use a sequence
anymore. I tried to use all
but then the problem is the additional element, since it has maxOccurs="unbounded"
.
How can I have an unordered list of elements with one of those those elements being always required and another element having unlimited occurrences?
Three suggestions. Either:
maxOccurs="unbounded"
is supported on xsd:all
.maxOccurs="unbounded"
. See additionalList
in the XSD below for a working example.<?xml version="1.0" encoding="utf-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="documents">
<xs:complexType>
<xs:all>
<xs:element name="invoice" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="report" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="additionalList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="additional" minOccurs="0" maxOccurs="unbounded">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With