0
<xs:element name="scheduled_times"  >
    <xs:complexType>
        <xs:sequence> 
            <xs:element ref="arrival_time"  />
            <xs:element ref="departure_time" />
        </xs:sequence>
    </xs:complexType>            
</xs:element>

This is mandating me to provide both "arrival_time" and "departure_time". But I want to be able to provide either or both. How can I achieve this?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
sheetal_158
  • 7,391
  • 6
  • 27
  • 44
  • Most likely user wants to ask how to edit the given schema structure so that it will allow omitting other one of the currently required elements but still keep the possibility to insert both elements. I have a solution for that, but currently it is not allowed to add new answers to this question. – jasso Nov 23 '14 at 01:16
  • 1
    If the asker clarifies the question so we know that is actually what they want, I will vote to reopen. – Dour High Arch Nov 23 '14 at 02:17
  • I agree. I can't justify casting a reopen vote based on a guess of what the OP might want, when the question doesn't actually say it. Please read this advice on asking good questions: [[ask]], [[Writing the perfect question](http://tinyurl.com/stack-hints)]. Pay special attention to the "Golden Rule", though I highly advise you to read the entire article. – Adi Inbar Nov 23 '14 at 02:51
  • 1
    Really, it's the original title that was problematic. Fixed to reflect narrowed scope. I easily enough covered the two reasonable interpretations of OP's question in my answer below. After re-opening and some obsolete comment removal (including this one), I believe our work is done here. Thanks. – kjhughes Nov 23 '14 at 15:52

1 Answers1

1

The easiest change to the XSD would be make both optional via minOccurs="0":

<xs:element name="scheduled_times">
    <xs:complexType>
        <xs:sequence> 
            <xs:element ref="arrival_time"  minOccurs="0"/>
            <xs:element ref="departure_time" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>            
</xs:element>

Note, though, that this will also allow neither to be present. The either, both, but not neither, case is a bit tricky because of the Unique Particle Attribution constraint.

Wrong - violates Unique Particle Attribution

Trying the straightforward approach of listing the desired possibilities via xs:choice:

  <xs:element name="scheduled_times">
    <xs:complexType>
      <xs:choice>
        <xs:sequence>
          <xs:element ref="arrival_time"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element ref="departure_time"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element ref="arrival_time"/>
          <xs:element ref="departure_time"/>
        </xs:sequence>
      </xs:choice>
    </xs:complexType>            
  </xs:element>

will run afoul of the Unique Particle Attribution constraint:

cos-nonambig: arrival_time and arrival_time (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.

Correct - does not violate Unique Particle Attribution

To resolve the ambiguity, re-work the choice so that the possibilities are unambiguous:

  <xs:element name="scheduled_times">
    <xs:complexType>
      <xs:choice>
        <xs:sequence>
          <xs:element ref="arrival_time"/>
          <xs:element ref="departure_time" minOccurs="0"/>
        </xs:sequence>
        <xs:element ref="departure_time"/>
      </xs:choice>
    </xs:complexType>            
  </xs:element>
kjhughes
  • 106,133
  • 27
  • 181
  • 240