Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I'm trying to set up part of a schema that's like a "Sequence" where all child elements are optional, but at least one of the elements must be present, and there could be more than one of them.

I tried doing the following, but XMLSpy complains that "The content model contains the elements <element name="DateConstant"> and <element name="DateConstant"> which cannot be uniquely determined.":

    <xs:choice>         <xs:sequence>             <xs:element name="DateConstant"/>             <xs:element name="TimeConstant"/>         </xs:sequence>         <xs:element name="DateConstant"/>         <xs:element name="TimeConstant"/>     </xs:choice> 

Can this be done (and if so, how)?

Some clarification: I only want to allow one of each element of the same name. There can be one "DateConstant" and/or one "TimeConstant", but not two of either. Gizmo's answer matches my requirements, but it's impractical for a larger number of elements. Hurst's answer allows two or more elements of the same name, which I don't want.

like image 727
Scott Leis Avatar asked Sep 19 '08 07:09

Scott Leis


People also ask

Which schema tag allows to specify elements in any order in XML?

xs:all specifies that the child elements can appear in any order.

What is XML Schema with example?

XML schema is a language which is used for expressing constraint about XML documents. There are so many schema languages which are used now a days for example Relax- NG and XSD (XML schema definition). An XML schema is used to define the structure of an XML document.


1 Answers

Try this:

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

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

This should do what you want, I hope.

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

like image 187
gizmo Avatar answered Sep 17 '22 22:09

gizmo