views:

570

answers:

2

My xml tag is given below

<ADCNT>
      <EM>
      <RUID>
</ADCNT>

I can make EM tag as mandatory also the same thing I can do with RUID by providing minOccurs = 1 (<EM minOccurs=1>). But I want if both of them are not present then do not validate the xml against schema. If any one of them is present then validate the xml against schema. Means if EM tag is not present then RUID tag must be present and vice versa.

So, how to solve this problem?

Thanks Sunil kumar Sahoo

+4  A: 

You need to make a choice:

<xs:element name="ADCNT">
  <xs:complexType>
    <xs:choice>
      <xs:sequence>
       <xs:element ref="EM" minOccurs="1"/>
       <xs:element ref="RUID" maxOccurs="0"/>
      </xs:sequence>
      <xs:sequence>
       <xs:element ref="RUID" minOccurs="1"/>
      </xs:sequence>
    </xs:choice>
  </xs:complexType>
</xs:element>
Martin v. Löwis
Haha, you posted almost the exact same schema (I embedded the `EM` and `RUID` elements directly instead of using a type reference) a few seconds before I did. Deleted my post.You'd expect there to be a more elegant way to say "exactly one of the elements out of of any of these", but I never heard of one.
Joren
Thanks for your help. I got the issue reolved.<xsd:element name="ADCNT"> <xsd:complexType> <xsd:choice minOccurs="1" maxOccurs="1"> <xsd:sequence> <xsd:element name="EM" minOccurs = "1"/> <xsd:element name="RUID" minOccurs = "0"/> </xsd:sequence> <xsd:sequence> <xsd:element name="RUID" minOccurs = "1"/> </xsd:sequence> </xsd:choice> </xsd:complexType> </xsd:element>
Deepak
A: 

Hi I got the issue resolved. Its like the way Martn v. Lowis mentioned but a little bit change

The below is the valid schema

Thanks Sunil Kumar Sahoo

Deepak
<xsd:element name="ADCNT"> <xsd:complexType> <xsd:choice minOccurs="1" maxOccurs="1"> <xsd:sequence> <xsd:element name="EM" minOccurs = "1"/> <xsd:element name="RUID" minOccurs = "0"/> </xsd:sequence> <xsd:sequence> <xsd:element name="RUID" minOccurs = "1"/> </xsd:sequence> </xsd:choice> </xsd:complexType> </xsd:element>
Deepak