How can I specify an XML schema for an instance document like this:
NOTE this answer is incorrect, as Serge pointed out.
Testing with xerces gives this error: type.xsd:3:21: cos-element-consistent: Error for type '#AnonType_productinfo'. Multiple elements with name 'informationset', with different types, appear in the model group.
There's more detail in the spec for cos-element-consistent.
But there's a solution, similar to Marc's answer below, but still using types. It is possible to have multiple occurrences of the same with different types, if they are in a minOccurs/maxOccurs list of a supertype, which is extended by the other types. That is, just like a list of polymorphic classes in java or C#. This overcomes the problem above because although that element name can appear many times in the xml, it only appears once in the xsd.
Here is example xsd and xml - tested with xerces this time!:
NOTE: You can't control the order of the different types, or how many times each type occurs (each could appear once, many times, or not appear at all) - just as with a list of polymorphic classes in java or C#. But you can at least specify at exact length of the list overall (if you like).
For example, I've restricted the above example to exactly two elements, but the order is not set (i.e. Manufacturer could be first, or Ingredients could be first); and number of repetitions is not set (i.e. they could both be Manufacturer, or both Ingredients, or one of each).
You can, with XML Schema type, as in:
And the XSD defines separate complex types for each one:
This is a special case for xsi:type
. In general, don't think you can specify attributes to have different values in elements of the same name, because they are different definitions of the same element.
I'm not 100% clear on the precise reason - anyone know the relevant part of the spec?