27

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>
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

7 Answers7

23

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 6
    This answer is the only one correct. What you need is array of nationality. All other methods are just hacks that breaks the principles of XML. – Alex Burtsev Feb 22 '14 at 08:25
21

Just come across the a similar problem (I wanted to have any number of each element in any order) and solved it with a sequence of choices. Using the example above:

<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name='user'>
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:choice>
          <xs:element name="name" type="xs:string" />
          <xs:element name="email" type="xs:string" />
          <xs:element name="nationality" />
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This allows you to have any number of name, email and nationality in any order.

fangster
  • 361
  • 2
  • 9
7

I think what you're looking for would go against the intent of XML. It would seems strange to have a valid XML fragment like this:

<user>
  <nationality/>
  <name/>
  <nationality/>
  <email/>
  <nationality/>
</user>

It sounds like you're asking for something like what marc_s proposed:

<user>
  <name/>
  <email/>
  <nationality/>
  <nationality/>
  <nationality/>
<user>

which needs to get pushed into:

<user>
  <name/>
  <email/>
  <nationalities>
     <nationality/>
     <nationality/>
     <nationality/>
  </nationalities>
<user>
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
3

Your code should be valid in XSD 1.1. For XSD 1.0 you have to find a workaround.

jelovirt
  • 5,844
  • 8
  • 38
  • 49
  • 28
    How does one use, enact, opt-into, create, covert, migrate, instantiate, find, or do XSD 1.1? – Ian Boyd Nov 22 '10 at 16:03
  • 3
    Could you cite the difference between XSD 1.1 and XSD 1.0 which makes it valid in 1.1 and invalid in 1.0? – Piotr Dobrogost Apr 02 '13 at 09:12
  • In the 1.1 spec I see no mention of any constraint making the OP's XML illegal. Sadly, I cannot find the 1.0 spec to be able to compare. My guess is that you'd have to check the version on the libs that will be parsing your XML/XSD to know whether you need any of the tricks below. – Luke Puplett Aug 21 '14 at 12:14
  • Okay, found it all, too long to cite here but see (all) under http://www.w3.org/TR/xmlschema-1/#Model_Group_details vs. the much shorter and less restrictive (all) under http://www.w3.org/TR/xmlschema11-1/#Model_Group_details – Luke Puplett Aug 21 '14 at 13:11
  • @Ian Boyd - 1.1 is backward compatible though 1.0 parsers will report false positives using 1.1 schemas. So I think its a case of the XML lib/validator authors updating to 1.1. This is not happening as quickly as I'd like. Even MarkLogic lacks support as of Feb 2014. – Luke Puplett Aug 21 '14 at 13:21
  • @IanBoyd - XSD 1.1 is currently supported by Saxon, Xerces J, and XML Spy. – C. M. Sperberg-McQueen Oct 07 '14 at 19:36
1

For XSD 1.0 the suggestion from leppie works.

The XSD

<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name='user'>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string" />
        <xs:element name="email" type="xs:string" />
        <xs:choice minOccurs='0' maxOccurs='unbounded'>
          <xs:element name="nationality" />
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

A sample XML document that validates against the schema

<user>
  <name>Name</name>
  <email>name@name.com</email>
  <nationality>Italian</nationality>
  <nationality>Japanese</nationality>
  <nationality>Alien</nationality>
</user>

And validation e.g. using xmllint

xmllint --noout --schema test.xsd test.xml
test.xml validate
ugo
  • 96
  • 3
  • 1
    There is no need in 'choice' in example you've provided. As far as you use 'sequence' element inside it can have maxOccurs="unbounded". But sequence imposes order of elements inside - that was the problem in initial question. – Vadym S. Khondar Jun 21 '12 at 14:25
  • On a positive note, this answer does show that model group elements `all`, `sequence` and `choice` can be nested. Similarly, in 1.1 at least, groups can be defined and referred to, see http://www.w3.org/TR/xmlschema11-1/#cModel_Group_Definitions which would avoid adding a new 'container' element as marc_s answer does. – Luke Puplett Aug 21 '14 at 12:25
0

Or, since "USER" will be set up with multiple child elements, why not set it up as a complex type? Something like this should work.

<xs:complexType>
  <xs:sequence>
    <xs:element name="Name" type="xs:string" />
    <xs:element name="Password" type="xs:string" />
    <xs:element minOccurs="1" maxOccurs="unbounded" name="Nationality" type="xs:string" />
  </xs:sequence>
</xs:complexType>
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
AllenG
  • 8,112
  • 29
  • 40
-1

xs:choice wont work? If not, just wrap that in a sequence or vice versa.

leppie
  • 115,091
  • 17
  • 196
  • 297