Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML schema can have multiple choices in a single complexType?

Tags:

java

xml

schema

xsd

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.

like image 280
Jimmy Avatar asked Jul 06 '11 21:07

Jimmy


People also ask

What is complexType in XML schema?

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.

How do you create a choice in XML schemas?

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.

What is true about XML schema Mcq?

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.

How many types of XML schema are there?

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.


1 Answers

<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>
like image 80
jasso Avatar answered Oct 13 '22 00:10

jasso