XSD: Different sub-elements depending on attribute/element value

Posted by AndiDog on Stack Overflow See other posts from Stack Overflow or by AndiDog
Published on 2010-05-11T19:27:26Z Indexed on 2010/05/11 19:34 UTC
Read the original article Hit count: 238

Filed under:
|

Another XSD question - how can I achieve that the following XML elements are both valid:

<some-element>
  <type>1</type>
  <a>...</a>
</some-element>

<some-element>
  <type>2</type>
  <b>...</b>
</some-element>

The sub-elements (either <a> or <b>) should depend on the content of <type> (could also be an attribute). It would be so simple in RelaxNG - but RelaxNG doesn't support key integrity :(

Is there a way to implement this in XSD?

Note: XML schema version 1.1 supports <xs:alternative>, which might be a solution, but afaik no reference implementation (e.g. libxml2) supports this yet. So I'm searching for workarounds. The only way I've come up with is:

<type>1</type>
<some-element type="1">
    <!-- simple <xs:choice> between <a> and <b> goes here -->
    <a>...</a>
</some-element>
<!-- and now create a keyref between <type> and @type -->

© Stack Overflow or respective owner

Related posts about xsd

Related posts about xml-schema