Search code examples
xmlxsdxsd-validationxml-validation

Mixing xsd:choice with xsd:any or xsd:sequence?


I have the following two (unchangeable) XML files with same namespace, which I need to create an XSD for:

pseudo-xml example #1:

<Project>
    <ProjectInformation/>
    <HistoryEntry/>
    <UserFiles/>
</Project>

pseudo-xml example #2:

<Project>
    <Installations/>
</Project>

Without the HistoryEntry and UserFiles element, I would use xsd:choice for ProjectInformation and Installations. But how can bring HistoryEntry and UserFiles element into the game?!

Is there a standard XSD mechanism that allows this?


Solution

  • Unordered is overrated. Just use xs:sequence rather than xs:any to avoid unique particle attribution violations in XSD 1.0:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Project">
        <xs:complexType>
          <xs:choice>
            <xs:sequence>
              <xs:element name="ProjectInformation"/>
              <xs:element name="HistoryEntry"/>
              <xs:element name="UserFiles"/>
            </xs:sequence>
            <xs:element name="Installations"/>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>