[ACCEPTED]-XML schema construct for "any one or more of these elements but must be at least one-schema

Accepted answer
Score: 26

Try this:

    <xs:element name="Elem1" />
    <xs:element name="Elem2" minOccurs="0" />
    <xs:element name="Elem3" minOccurs="0" />
    <xs:element name="Elem2" />
    <xs:element name="Elem3" minOccurs="0" />
  <xs:element name="Elem3" />

Doing so, you force either to choose 7 the first element and then the rest is optional, either 6 the second element and the rest is optional, either 5 the third element.

This should do what you 4 want, I hope.

Of course, you could place 3 the sub-sequences into groups, to avoid 2 to duplicate an element in each sequence 1 if you realize you miss one.

Score: 19

According to the technical article on MSDN 8 titled Understanding XML Schema at http://msdn.microsoft.com/en-us/library/aa468557.aspx#understandxsd_topic5 you can take advantage of constraints 7 such as minOccurs on the choice definition (compositor) itself:

"Using occurrence constraints on a compositor applies to the entire group as a whole"

(See 6 the more sophisticated example that uses 5 nested complex types and the AuthorType 4 example)

You stated your requirement as "at 3 least one of the elements must be present, and 2 there could be more than one of them". Thus, I 1 propose you try the following:

<xs:choice minOccurs="1" maxOccurs="unbounded">
    <xs:element name="DateConstant" type="..."/>
    <xs:element name="TimeConstant" type="..."/>
Score: 2


Unfortunately you have failed to 26 understand the original question. Placing 25 minOccurs="1" on the choice is satisfied 24 automatically when ALL elements that have 23 minOccurs="0" are contained as options.

Thus 22 you have failed to account for the "at least 21 one" required by the original poster, because 20 no elements correctly satisfies 1 occurrance 19 of two completely optional elements.

So far 18 I am unable to find a solution to this as 17 minOccur/maxOccur both relate to the group 16 in which they are defined and DO NOT relate 15 to an overall number of nodes. Nor can you 14 use the choice element to define the same 13 named element more than once or it becomes 12 "ambiguous". I have seen some examples use 11 references instead of elements of a specific 10 type, but I believe this fails the microsoft 9 XSD parser.

<xs:choice minOccurs="1" maxOccurs="1">
  <xs:sequence minOccurs="1" maxOccurs="1">
    <xs:element name="Elem1" minOccurs="1" maxOccurs="1" />
    <xs:element name="Elem2" minOccurs="0" maxOccurs="1" />
  <xs:sequence >
    <xs:element name="Elem2" minOccurs="1" maxOccurs="1" />

Here you can see that either 8 you have the first sequence (which MUST 7 have Elem1 but may have Elem2 optionally), or 6 you have the second sequence (which MUST 5 have Elem2).

Hence you now have "any one 4 or more" of these 2 elements. Of course 3 this gets exponentially more complex the 2 more options you have as you need to provide 1 additional choices for all possible combinations.

More Related questions