xml - How to make one of two optional tag dependent on the other? -


i have 2 tags in xsd:

<xsd:element name="tag1" minoccurs="0">  <xsd:element name="tag2" minoccurs="0"> 

they both optional , can omitted. want add restriction if tag2 given, tag1 should given (as tag2 dependent on tag1). how achieve xsd?


edit:

there mandatory tags between them. sequence not work. e.g.

<xsd:element name="tag1" minoccurs="0"> <xsd:element name="tag3" minoccurs="1"> <xsd:element name="tag4" minoccurs="1"> <xsd:element name="tag2" minoccurs="0"> 

here solutions:

use choice

this long solution repeats parts of model works if need mandatory tags between optional tags , tags not inside xs:all:

<xsd:choice>      <!-- choice option 1: optional tags present -->     <xsd:sequence>         <xsd:element name="optionaltag1"/>         <xsd:element name="complulsorytag1"/>         <xsd:element name="complulsorytag2"/>         <xsd:element name="optionaltag2"/>     </xsd:sequence>      <!-- choice option 2: optional tags not present -->     <xsd:sequence>         <xsd:element name="complulsorytag1"/>         <xsd:element name="complulsorytag2"/>     </xsd:sequence>  </xsd:choice> 

note can avoid repeating tags on model if use xs:group group complusory central tags.

wrap tags in optional sequence

if there not mandatory tags between them wrap them in sequence minoccurs=0. if sequence appears both tags present, , if sequence doesn't appears none of tags present:

<xsd:sequence minoccurs="0">     <xsd:element name="tag1"/>     <xsd:element name="tag2"/> </xsd:sequence> 

note won't work inside xs:all can use inside choice or inside sequence if want.

use xsd 1.1 assertions

if processor using xsd 1.1 can use xs:assert ensure either of optional tags present or either none of them present:

<xsd:complextype>     <xsd:sequence>         <xsd:element name="optionaltag1" minoccurs="0"/>         <xsd:element name="complulsorytag1"/>         <xsd:element name="complulsorytag2"/>         <xsd:element name="optionaltag2" minoccurs="0"/>     </xsd:sequence>     <!-- both optional tags present or none of them present -->     <xsd:assert test="boolean(optionaltag1) = boolean(optionaltag2)"/> </xsd:complextype> 

note 1 of solution presenteds work xs:all.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -