tags:

views:

453

answers:

1

I am trying to create an XSD schema for the following XML:

<root>
  <!-- The actual file must contain one of the following constraints -->
  <constraint type="interval">
    <min>100</min>
    <max>200</max>
  </constraint>

  <constraint type="equals">
    <value>EOF</value>
  </constraint>
</root>

The child elements of the constraint element depends on the value of the type attribute.

I have successfully validated the XML using an abstract type defining the type attribute, and two extending types defining the child elements. This would require me to decorate the XML with an xsi:type attribute, naming the actual extending type:

  <constraint type="interval" xsi:type="intervalConstraintType">
    <min>100</min>
    <max>200</max>
  </constraint>

Sadly, I'm not in control of the XML-structure and new attributes will be hard to introduce.

Is this doable with XSD? Are there alternatives that are more suitable?

+1  A: 

I think it should be possible, but currently do not know how to it myself. As a workaround you could rewrite the xml on the fly to include your extension.

Edit: Hmm, looks like it is not possible, at least not in XSD 1.0

Simon Groenewolt
How would you rewrite the xml on the fly?
opto
I was assuming you were processing it in some programming language, in that case you create a local copy of the xml that you are (presumably) loading from another source and make the modifications on the local copy.
Simon Groenewolt