Is it possible to do something like this in an XML schema?
<xsd:complexType name="ItemsType">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="shirt"/>
<xsd:element ref="hat"/>
<xsd:element ref="umbrella"/>
</xsd:choice>
<xsd:choice minOccurs="1" maxOccurs="3">
<xsd:element ref="apple"/>
<xsd:element ref="banana"/>
<xsd:element ref="strawberry"/>
</xsd:choice>
</xsd:complexType>
this is apparently invalid though. What I would like is for it to be possible to have 0 or more of the first choice.. E.g. there could be a shirt element and a hat element, or perhaps no clothes elements at all (since minOccurs="0"), followed by at least 1 fruit elements (I want to make it so there has to be at least one, since minOccurs="1").
is there a way to do it?
Thanks for any help.
A complex type is essentially a type definition for elements that may contain attributes and elements. An element can be declared with a type attribute that refers to a complexType element that defines the structure, content, and attributes of that element.
Using <xsd:choice> in an XSD It has to be defined in XML schema definition of a nested XML content. The element in the root schema has to be optional. Add attribute minOccurs="0" on the element to make it optional.
XML schema defines the elements, attributes and data types. C. It is similar to a database schema that describes the data in a database. Explanation: All of the above statement are true.
Supported XML Schema data types The monitor model supports eight XML Schema data types. Expressions with these argument and result types are created using the XML Path Language (XPath) 2.0.
<xsd:complexType>
expects to have only one child element. Wrap your two choices inside a single <xsd:sequence>
.
Example
<xsd:complexType name="ItemsType">
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
... clothes ...
</xsd:choice>
<xsd:choice minOccurs="1" maxOccurs="3">
... fruits ...
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
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