1

Consider the two scenarios:

I have one XML that looks like:

<personinfo>
   <info> 
        <option1>Coke</option1>
   </info>
</personinfo>

where I should have a choice between an option1 and option2 element.

I have another XML that looks like:

 <personinfo>
       <info> 
            <firstname>Yair</firstname>
            <lastname>Zaslavsky</lastname>
       </info>
    </personinfo>

where both firstname and lastname should appear (hence a sequence).

I have tried to use the following schema:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="optionsChoice">
    <xs:choice>
      <xs:element name="option1" type="xs:string"/>
      <xs:element name="option2" type="xs:string"/>
    </xs:choice>
  </xs:complexType>

  <xs:complexType name="optionsSequence">
     <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
  </xs:complexType>

<xs:complexType name="personinfo">
  <xs:choice>
      <xs:element name="info" type="optionsSequence"/>
      <xs:element name="info" type="optionsChoice"/>
    </xs:choice>
</xs:complexType>


</xs:schema>

With no luck.

Bear in mind that I must have "info" as an element name in both cases.

How can I fix this issue?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27

1 Answers1

1

You cannot have two elements with the same name but different types appear in a content model together.

You can, however, push the choice down such that personinfo can be either a choice of option1 or option2 or a sequence of firstname and lastname:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="personinfo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="info">
          <xs:complexType>
            <xs:choice>
              <xs:choice>
                <xs:element name="option1" type="xs:string"/>
                <xs:element name="option2" type="xs:string"/>
              </xs:choice>
              <xs:sequence>
                <xs:element name="firstname" type="xs:string"/>
                <xs:element name="lastname" type="xs:string"/>
              </xs:sequence>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This XSD would valid both of your XML documents successfully.

kjhughes
  • 106,133
  • 27
  • 181
  • 240