title: Different sub-elements depending on attribute/element value
body: <p>Another XSD question - how can I achieve that the following XML elements are both valid:</p>
  
  <pre><code>&lt;some-element&gt;
    &lt;type&gt;1&lt;/type&gt;
    &lt;a&gt;...&lt;/a&gt;
  &lt;/some-element&gt;
  
  &lt;some-element&gt;
    &lt;type&gt;2&lt;/type&gt;
    &lt;b&gt;...&lt;/b&gt;
  &lt;/some-element&gt;
  </code></pre>
  
  <p>The sub-elements (<em>either</em> <code>&lt;a&gt;</code> <em>or</em> <code>&lt;b&gt;</code>) should depend on the content of <code>&lt;type&gt;</code> (could also be an attribute). It would be so simple in RelaxNG - but RelaxNG doesn't support key integrity :(</p>
  
  <p>Is there a way to implement this in XSD?</p>
  
  <p>Note: XML schema version 1.1 supports <code>&lt;xs:alternative&gt;</code>, 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:</p>
  
  <pre><code>&lt;type&gt;1&lt;/type&gt;
  &lt;some-element type="1"&gt;
      &lt;!-- simple &lt;xs:choice&gt; between &lt;a&gt; and &lt;b&gt; goes here --&gt;
      &lt;a&gt;...&lt;/a&gt;
  &lt;/some-element&gt;
  &lt;!-- and now create a keyref between &lt;type&gt; and @type --&gt;
  </code></pre>