Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Middle way between XSD all and XSD sequence

Tags:

xml

xsd

I'm defining a user element with XSD. For this example, a user has a name, email and one or more nationalities. I've tried:

<xs:all>   <xs:element name="name" blabla />   <xs:element name="email" blabla />   <xs:element name="nationality" minOccurs="1" maxOccurs="unbounded" /> </xs:all> 

However, that is illegal. Apparently elements inside an "All" can only occur one time (or not at all). I could fix this by changing the All to a Sequence, but then people would have to enter the properties in the exact order, which I actually don't care about.

Is there a combination of these two available? Not according to http://www.w3schools.com/Schema/schema_complex_indicators.asp, but maybe it's hidden (or my inexperienced eyes don't see it).

By intuition, I also tried:

<xs:all>   <xs:element name="name" blabla />   <xs:element name="email" blabla />   <xs:sequence>     <xs:element name="nationality" minOccurs="1" maxOccurs="unbounded" />   </xs:sequence> </xs:all> 

But that's unfortunately invalid.


Here is the current, real, piece of XSD:

  <!-- user -->   <xs:complexType name="user">     <xs:sequence>       <xs:element name="firstname" type="xs:string" minOccurs="1" maxOccurs="1" />       <xs:element name="appendix" type="xs:string" minOccurs="0" maxOccurs="1" />       <xs:element name="lastname" type="xs:string" minOccurs="1" maxOccurs="1" />       <xs:element name="address" type="xs:string" minOccurs="1" maxOccurs="1" />       <xs:element name="zipcode" type="xs:string" minOccurs="1" maxOccurs="1" />       <xs:element name="city" type="xs:string" minOccurs="1" maxOccurs="1"/>       <xs:element name="username" type="xs:string" minOccurs="1" maxOccurs="1"/>       <xs:element name="email" type="xs:string" minOccurs="1" maxOccurs="1"/>       <xs:element name="country" type="country" minOccurs="1" maxOccurs="1"/>       <xs:element name="nationality" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>     </xs:sequence>   </xs:complexType> 
like image 439
Bart van Heukelom Avatar asked May 08 '09 09:05

Bart van Heukelom


People also ask

What is the difference between the sequence and all schema elements?

Difference: xsd:all - "child elements can appear in any order and each child element can occur zero or one time" (ie, maxOccurs can be 0 or 1) xsd:sequence - "child elements must appear in a sequence. Each child element can occur from 0 to any number of times" (ie, maxOccurs can be 0 or any number or 'unbounded')

What is minOccurs and maxOccurs in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is complexType in XSD?

The complexType element defines a complex type. A complex type element is an XML element that contains other elements and/or attributes.


1 Answers

Could you just turn your "nationality" thingie into its own complexType and then use that new complex type inside your xs:all?

<xs:complexType name="NationalityType">   <xs:sequence>        <xs:element name="nationality" minOccurs="1" maxOccurs="unbounded" />   </xs:sequence> </xs:complexType>  <xs:all>   <xs:element name="name" blabla />   <xs:element name="email" blabla />   <xs:element name="nationalities" type="NationalityType" /> </xs:all> 

I don't have anything at hand to test this, so this is really just off the top of my head..... give it a try!

EDIT: tested it by now - it works, the only minor price to pay is that your XML will have to look something like this:

<....>   <email>......</email>   <nationalities>     <nationality>ABC</nationality>     <nationality>CDE</nationality>   </nationalities>   <name>.....</name> </.....> 

So you get an extra node that will contain the arbitrary long list of <nationality> items.

Marc

like image 161
marc_s Avatar answered Sep 18 '22 22:09

marc_s