XML schema - how do you conditionally need address elements? (street, city, state, etc.)

If the address can consist of children: Street, City, State, PostalCode ... how do you enable this XML:

 <Address>
         <Street>Somestreet</Street> 
         <PostalCode>zip</PostalCode>                     
 </Address>

and enable this:

<Address>
     <City>San Jose</City>
     <Street>Somestreet</Street> 
     <State>CA</State>
</Address>

but not :

<Address>
    <Street>Somestreet</Street> 
    <City>San Jose</City>
</Address>

What circuit will do such things ??

+3
source share
1 answer

Using choicethere is a complicated way to create a choice where only valid combinations are allowed ...

In your example, this should have the desired result:

 <xs:complexType name="Address">
  <xs:choice>
   <xs:sequence>
    <xs:element name="city"/>
    <xs:element name="street"/>
    <xs:element name="state"/>
   </xs:sequence>
   <xs:sequence>
    <xs:element name="street"/>
    <xs:element name="postcode"/>
   </xs:sequence>
  </xs:choice>
 </xs:complexType>

Another simple example if you want to resolve any two of three. You could do this, say you have ABC elements, and you want to allow any two of the three, you can use the following xsd:

<xs:complexType name="anyTwo">
  <xs:choice>
   <xs:sequence>
    <xs:element name="A"/>
    <xs:element name="B"/>
   </xs:sequence>
   <xs:sequence>
    <xs:element name="A"/>
    <xs:element name="C"/>
   </xs:sequence>
   <xs:sequence>
    <xs:element name="B"/>
    <xs:element name="C"/>
   </xs:sequence>
  </xs:choice>
 </xs:complexType>

, , !

: .

+6

Source: https://habr.com/ru/post/1748691/


All Articles