views:

69

answers:

2

Is it possible in some way, to define an xsd scheme which could validate such xml:

<item_list>
  <item ItemType="SimpleMessage" Caption="Simplest message"/>
  <item ItemType="ComplexMessage" SomeAttr="value">
    <item_data>some text</item_data>
  </item>
</item_list>

Problem is that i havn't find any possibility to define smth like:

  <xsd:element name="Items">
      <xsd:complexType>
        <xsd:choice>
          <xsd:element name="item" type="SimpleMessType"/>
          <xsd:element name="item" type="ComplexMessType"/>
        </xsd:choice>
      </xsd:complexType>
  </xsd:element>

But i need to check, that SimpleMessage has no child elements or additional attrs :(

A: 

You cannot do this using the schema structure you proposed, because the structure violates the XML Schema ambiguity rules.

One potential option for you is to define a super-type, say BaseElement, that is empty, and then sub-types, and use xsi:type to override instead of just a normal type attribute. More information on how that works can be found here.

xcut
A: 

XSD expressly prohibits such a case. You must change the element names to be unique (or use xsi:type as xcut says, which amounts to the same thing).

As a work-around, you could merge the type definitions SimpleMessType and ComplexMessType into a single type with mixed="true" -- and then disentangle the content you receive in your own code after Schema processing is complete. See the stackoverflow discussion about XSD schema for recursive XML.

WReach