• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2044
  • Last Modified:


In my XSD,  i've got two elements, I want at most 1 of either, and at least 1 of them.
In other words I want : a, ab, abc, b,ba,bc, etc..  but not aa or bb, etc or nothing.
I tried:
    <xs:complexType name="xType">
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:element ref="a"/>
            <xs:element ref="b"/>
            <xs:element ref="c"/>

but this doesnt allow ab, etc

<xs:complexType name="xType">
       <xs:choice minOccurs="1" maxOccurs="unbounded">
           <xs:element ref="a" maxOccurs="1"/>
           <xs:element ref="b" maxOccurs="1"/>
           <xs:element ref="c" maxOccurs="1"/>

but this allows aa,bb etc..

I also tried:
    <xs:complexType name="xType">
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element name="a" minOccurs="0" maxOccurs="1"/>
            <xs:element name="b" minOccurs="0" maxOccurs="1"/>
            <xs:element name="c" minOccurs="0" maxOccurs="1"/>

But this doesn't enforce the non-empty condtion.

How can this requirement be implemented in XSD?
  • 2
1 Solution
Geert BormansCommented:
by implementing the different options, the risk of combining xs:all and xs:choice will make your model ambiguous soon,
so your best bet is giving the options by combining sequences and choices and do that carefully

(a, (b? & c?)) | (b, (a? & c?)) | (c, (b? & a?))

is unambiguous, gives at least one element, up to three and no element is allowed twice
You can vary on this theme to get what you want

In my opinion it is better to model as an xs:choice multiple times
(a | b | c)+
makes sure that you have at least one element in the content model
and use schematron for restricting (www.Schematron.com)
in the restricting assertions you can say something like
"count(a) <= 1 and count(b) <= 1 and count(c) <= 1"
that should cover your requirement
Geert BormansCommented:
in short:
your only option, without bringing schematron in the picture, is listing all permutations one way or another
I would go the schematron route by the way

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now