Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Schema order of elements

Tags:

xsd

In my XSD, I want to be able to specify that the order of the elements doesn't matter. This is what I have:

<xs:element name="ADT_A08_231_GLO_DEF">
  <xs:complexType>
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="EVN_EventTypeSegment" type="xs:string" />
      <xs:element minOccurs="1" maxOccurs="1" name="PID_PatientIdentificationSegment" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="PD1_PatientAdditionalDemographicSegment" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

How can I make it so that the EVN and PID element can occur random (first EVN then PID or first PID element and then the EVN element) in the XML file?

<EVN_EventTypeSegment>Test</EVN_EventTypeSegment>
<PID_PatientIdentificationSegment>PIDTest</PID_PatientIdentificationSegment>

or:

<PID_PatientIdentificationSegment>PIDTest</PID_PatientIdentificationSegment>
<EVN_EventTypeSegment>Test</EVN_EventTypeSegment>
like image 450
Rise_against Avatar asked Oct 19 '10 19:10

Rise_against


People also ask

Does the order of elements in XML matter?

XML Specification Generally speaking, the order in which child elements appear inside their parent element container in XML shouldn't matter. As such, the following two examples would be considered semantically equivalent XML.

What is sequence in XML schema?

Definition and Usage The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.

Is XML order dependent?

Element order is significant in XML. The grammar implies that element ordering is significant, but, unlike for attributes, there is no explicit statement regarding the significance of the order of elements.


2 Answers

Use xs:all instead of xs:sequence.

like image 130
Yaro Avatar answered Oct 20 '22 16:10

Yaro


Change the xs:sequence in your schema document to xs:all. An all-group containing references to (or declarations of) elements A, B, and C is satisfied if and only if A, B, and C are present in some order. The elements may have minOccurs set to 0 to make them optional (like your PD1_PatientAdditionalDemographicSegment element).

In XSD 1.0, the children of an all-group must have maxOccurs of 1, which some people find uncomfortably restrictive, but in your case that's what you want anyway. In XSD 1.1 that restriction is lifted.

like image 23
C. M. Sperberg-McQueen Avatar answered Oct 20 '22 15:10

C. M. Sperberg-McQueen