Elements with maxOccurs (some unbound, some 1)

F

foolproofplan

Maybe I am looking to far into a simple problem, but I am having quite
a hard time figuring this out:

I am currently trying to create a schema to represent an Element which
contains other Elements (some of which have a restriction on how many
can occur). Here is an example of what I have done if that seems
confusing:

<xsd:element name="Object">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element ref="License" minOccurs="0"/>
<xsd:element ref="NameMemberReference" minOccurs="0"
maxOccurs="1"/>
<xsd:element ref="ParentMemberReference" minOccurs="0"
maxOccurs="1"/>
<xsd:element ref="Members"/>
<xsd:element ref="OnEdit" minOccurs="0"/>
<xsd:element ref="OnCommit" minOccurs="0"/>
<xsd:element ref="Permissions" minOccurs="0"/>
</xsd:choice>
<xsd:attribute ref="name" use="required"/>
</xsd:complexType>
</xsd:element>

I think the trouble with this is that the two elements that need to be
restricted to one occurance (NameMememberReference and
ParentMemberReference) are within an unbounded choice so they still
can be created multiple times.

I've played around with some other idea by using a Choice and an All
and I've also trying making groups but nothing has seemed to solve my
problem. Any ideas?

Thanks,
- Marc
 
J

Joseph Kesselman

Any ideas

Do you really need to leave the order unconstrained? If you're willing
to nail down the order slightly, factor the two maxOccurs='1' items out
and enclose them and the choice in a sequence.

Or make the whole thing a sequence and just require folks to give the
contents in that specific order, if there isn't a good reason to let
them randomly intermix the contents.
 
F

foolproofplan

Joseph Kesselman said:
Do you really need to leave the order unconstrained?

Yep, the order is going to have to be left open. It's something beyond
my control.
 
J

Joseph Kesselman

If it was just a single constrained element, I'd suggest breaking it up
into submodels -- any mixture of the others, that one, any mixture of
the others.

With two, and being unwilling to constrain their order... I think you
can do it by combining that idea with Martin's solution in the thread
entitled "Xml Schema OR, XOR", breaking things down into non-redundant
cases. But you're going to wind up with a lot of copy-and-repeat Schema
code to express that.

There may be a better solution; my schema skills aren't supurb.

There's always the alternative of applying the at-most-one constraint in
applications (semantic validation) rather than in schemas (higher-level
syntactic validation).
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top