cgsabari
cgsabari

Reputation: 615

Confuse with xsd schema and xml file elements

I've one schema xsd file. In that xsd file, i've confused with "choice".

For e.g myschema.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Emp">
    <xs:complexType mixed="true">
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="E1" />
        <xs:element ref="E2" />
        <xs:element ref="E3" />
        <xs:element ref="E4" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

myxmlfile1.xml - e.g 1

EDIT: Root element Employee added.

<?xml version="1.0" encoding="utf-8"?>
<Employee>
  <Emp>
    <E1></E1>
    <E2></E2>
  </Emp>
</Employee>

myxmlfile2.xml - e.g 2

EDIT: Root element Employee added.

<?xml version="1.0" encoding="utf-8"?>
<Employee>
  <Emp>
    <E1></E1>
  </Emp>
  <Emp>
    <E2></E2>
  </Emp>
</Employee>

In these 2 xml files, according to schema, myxmlfile1.xml is correct else myxmlfile2.xml is correct??

clear me with explanation...

EDIT:

Here i want to know, 

Upvotes: 0

Views: 80

Answers (2)

Michael Kay
Michael Kay

Reputation: 163587

Think of xs:choice with maxOccurs=unbounded as meaning you can have as many elements as you like, and each element must be either a foo or a bar or a baz (or whatever the list in your xs:choice happens to be).

Upvotes: 2

Pavel Furs
Pavel Furs

Reputation: 11

First of all, the second example is neither valid nor well-formed xml(XML cannot contain more than one root element).

The first one is fine, because of xs:choice and maxOccurs="unbounded" used together.

xs:choice forces you to select just one of the provided elements, while maxOccurs="unbounded" allows you to do it as much times as you want.

Order does not matter in this case, e.g. such document will be valid as well:

<?xml version="1.0" encoding="utf-8"?>
<Emp>
  <E4></E4>
  <E4></E4>
  <E2></E2>
  <E3></E3>
</Emp>

Upvotes: 1

Related Questions