xsd puzzler

A

Anon

Hi,

Having trouble creating a schema. I want the following chunks xml to be
acceptable

<a>
<b .... />
</a>

...or...

<a>
<c .... />
</a>

So element a can can have sub-elements b or c.

Constraints are: (1) a must have one of b or c, (2) a may only have one
of b or c.

The current schema I've got is as follows:

<xs:choice maxOccurs="1" minOccurs="1">
<xs:element name="b" type="typeB" minOccurs="0"/>
<xs:element name="c" type="typeC" minOccurs="0"/>
</xs:choice>

However, although this schema satisfies constraint (2), it allows me to
create element <a> without any sub-elements. In other words, the
minOccurs of the choice is being ignored.

Can anyone suggest an alternate approach?

If it makes any difference I'm using XML Spy to create all of this, and
will deploy to the MS Xml parser (whatever version is latest).

TIA,
Pete
 
P

Patrick TJ McPhee

% Having trouble creating a schema. I want the following chunks xml to be
% acceptable
%
% <a>
% <b .... />
% </a>
%
% ..or...
%
% <a>
% <c .... />
% </a>

[...]

% The current schema I've got is as follows:
%
% <xs:choice maxOccurs="1" minOccurs="1">
% <xs:element name="b" type="typeB" minOccurs="0"/>
% <xs:element name="c" type="typeC" minOccurs="0"/>
% </xs:choice>

So, why do you have minOccurs set to 0? If you just use the
default values of minOccurs and maxOccurs, this should do
exactly what you want.
 
B

Bob Foster

Remove the two minOccurs="0" as previously suggested.

minOccurs="1" maxOccurs="1" on the choice (which, by the way, is the
default) does not mean, as you may think, that the choice is constrained to
output exactly one element, it means the choice is not optional. But the
absence of any child elements matches both alternatives, so it is accepted.

Maybe the XML syntax is confusing. If you wrote (b?|c?) I don't think you'd
be surprised if it allowed no elements. Anyway, you shouldn't be.

Bob Foster
 
P

Pete

My code now works, and my understanding of MinOccurs has been increased.

Thanks for your advice.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top