0

I have XMLs where some elements must be present 0 or 1 times, others can occur 0..many times. Example: A Person element can have 'firstName'(0..1), 'lastName'(0..1), Pet(0..m), Address(0..m) The order of any of those is not predictable. Example of valid XML:

<Person>
    <Pet>Cat1</Pet>
    <Address>addres 1 bla bla</Address>
    <firstName>Bob</firstName>
    <Pet>Dog1</Pet>
    <lastName>McWilliams</lastName>
    <Address>another address</Address>
</Person>

I tried SCHEMA-1:

<xs:element name="Person">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="firstName" type="xs:string"/>
        <xs:element name="lastName" type="xs:string"/>
        <xs:element name="Pet" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="Address" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

That does not work as it expected elements in particuar order, firstName first, lastName second, etc. Given XML would not pass validation.

I also tried SCHEMA-2:

<xs:element name="Person">
    <xs:complexType>
      <xs:all>
        <xs:element name="firstName" type="xs:string"/>
        <xs:element name="lastName" type="xs:string"/>
        <xs:element name="Pet" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="Address" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      </xs:all>
    </xs:complexType>
  </xs:element>

This does not work because, apparently children of 'all' cannot have maxOccurs>1, which is a problem in my case as I need to accept any number of Pets or Addresses. I cannot easily change structure of XMLs (provided by third-party).

How can I make such schema that would validate number of element occurrences but completely ignore order?

EasternDude
  • 413
  • 4
  • 16

0 Answers0