简体   繁体   中英

xml xsd complextype with mandatory elements and arbitrary order

I am trying to write a complextype schema containing mandatory elements but in arbitrary order. My complextype looks like this:

   <xs:choice minOccurs="0" maxOccurs="1">
      <xs:element name="link" type="xs:unsignedInt" minOccurs="1" maxOccurs="1"/>
      <xs:element name="Plan" type="xs:unsignedInt" minOccurs="1" maxOccurs="1"/>
    </xs:choice>

the xml file will be valid if I enter just one of the two elements(while it shouldn't be valid):

<link>123<link/>

or

<Plan>222<Plan/>

are valid while I want only the followings to be valid:

<link>123<link/>
<Plan>222<Plan/>

or

 <Plan>222<Plan/>
 <link>123<link/>

can you help me please? thank you

        <xs:choice minOccurs="0" maxOccurs="1">
        <xs:sequence>
            <xs:element name="link" type="xs:unsignedInt" minOccurs="1" maxOccurs="1"/>
            <xs:element name="Plan" type="xs:unsignedInt" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
        <xs:sequence>               
            <xs:element name="Plan" type="xs:unsignedInt" minOccurs="1" maxOccurs="1"/>
            <xs:element name="link" type="xs:unsignedInt" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
        </xs:choice>

Can solve the issue but if this is just pseudo sample code then try with <xs:group> and <xs:choice>

If each element can occur at most once, use xs:all:

 <xs:all>
   <xs:element name="link" type="xs:unsignedInt" minOccurs="1" maxOccurs="1"/>
   <xs:element name="Plan" type="xs:unsignedInt" minOccurs="1" maxOccurs="1"/>
 </xs:all>

With XSD 1.1 this also works when elements can be repeated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM