Come si può fare una sequenza di elementi opzionali che devono essere in ordine in uno schema XML?

StackOverflow https://stackoverflow.com/questions/931622

  •  06-09-2019
  •  | 
  •  

Domanda

Quello che voglio fare è creare un elemento sequenza in uno schema XML in modo tale che i contenuti devono essere in ordine, ma non può essere tutti presenti. Ad esempio, lo schema sarebbe qualcosa di simile a questo:

<xs:element name="rods" maxOccurs="1" minOccurs="1">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="green" type="xs:positiveInteger" />
   <xs:element name="white" type="xs:positiveInteger" />
   <xs:element name="blue" type="xs:positiveInteger" />
   <xs:element name="yellow" type="xs:positiveInteger" />
   <xs:element name="red" type="xs:positiveInteger" />
   <xs:element name="tan" type="xs:positiveInteger" />
   <xs:element name="gray" type="xs:positiveInteger" />
   <xs:element name="black" type="xs:positiveInteger" />
  </xs:sequence>
 </xs:complexType>
</xs:element>

e permetterebbe di XML in questo modo:

<rods>
 <green>142</green>
 <white>34</white>
 <gray>65</gray>
</rods>

MSDN ha questo da dire in proposito:

  

Il seguente esempio mostra un elemento (zooAnimals) che può avere zero o più dei seguenti elementi, elefante, muniti, giraffe, nell'elemento sequenza.

<xs:element name="zooAnimals">
      <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
               <xs:element name="elephant"/>
               <xs:element name="bear"/>
               <xs:element name="giraffe"/>
            </xs:sequence>
      </xs:complexType>
</xs:element>

W3Schools suggerisce lo stesso. Tuttavia, sia in studio visivo e un servizio di validazione online non piace ciò che viene suggerito.

Ecco quello che ho in questo momento:

<xs:element name="rods" maxOccurs="1" minOccurs="1">
 <xs:complexType>
  <xs:sequence minOccurs="0" maxOccurs="1">
   <xs:element name="green" type="xs:positiveInteger" />
   <xs:element name="white" type="xs:positiveInteger" />
   <xs:element name="blue" type="xs:positiveInteger" />
   <xs:element name="yellow" type="xs:positiveInteger" />
   <xs:element name="red" type="xs:positiveInteger" />
   <xs:element name="tan" type="xs:positiveInteger" />
   <xs:element name="gray" type="xs:positiveInteger" />
   <xs:element name="black" type="xs:positiveInteger" />
  </xs:sequence>
 </xs:complexType>
</xs:element>

Ed ecco l'XML:

<rods>
 <green>142</green>
 <white>34</white>
 <gray>65</gray>
</rods>

Visual Studio 2005 sostiene che "grigia" non è valido, "blu" previsto, così chiaramente che vuole che tutti i bambini

Grazie,

Eric

È stato utile?

Soluzione

<xs:element name="rods">
    <xs:complexType>
        <xs:sequence>
                <xs:element minOccurs="0" name="green" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="white" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="blue" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="yellow" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="red" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="tan" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="gray" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="black" type="xs:positiveInteger" />
        </xs:sequence>
    </xs:complexType>
</xs:element>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top