title: descendant constraint conditional on top-level ancestor in XML Schema 1.0
body: <p>I would like to have a descendant element conditional on the top-level ancestor type. Is it possible to model that relationship in xml schema 1.0? If so, how?</p>
  
  <p>Below is my desired structure/validation behavior:</p>
  
  <pre><code>&lt;a1&gt;
    &lt;b&gt;
      &lt;c&gt;
        &lt;d/&gt; -- allowed if ancestor is a1
      &lt;/c&gt;
    &lt;/b&gt;
  &lt;/a1&gt;
  
  &lt;a2&gt;
    &lt;b&gt;
      &lt;c&gt;
        &lt;d/&gt; -- validation error - not allowed if ancestor is not a1
      &lt;/c&gt;
    &lt;/b&gt;
  &lt;/a2&gt;
  </code></pre>
  
  <p>It seems that XSD 1.1 assertions would enable me to do just that, but I'm stuck with XML Schema 1.0.</p>
  
  <p>I could obviously make parallel hierarchies and have the d element only allowed in one but that gets verbose and confusing for users of our schema.</p>
  
  <p>The parallel hierarchy could look like this:</p>
  
  <pre><code>&lt;a1&gt;
    &lt;b1&gt;
      &lt;c1&gt;
        &lt;d/&gt; -- allowed in c1 element
      &lt;/c1&gt;
    &lt;/b1&gt;
  &lt;/a1&gt;
  
  &lt;a2&gt;
    &lt;b2&gt;
      &lt;c2&gt;
        &lt;d/&gt; -- not allowed in c2
      &lt;/c2&gt;
    &lt;/b2&gt;
  &lt;/a2&gt;
  </code></pre>
  
  <p><b> Edit: Solution schema to above question using CM's tip </b></p>
  
  <pre><code>&lt;xs:element name="a1"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="b" type="b1"&gt;&lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:element name="a2"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="b" type="b2"&gt;&lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:complexType name="b1"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="c" type="c1"&gt;&lt;/xs:element&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  &lt;xs:complexType name="b2"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="c" type="c2"&gt;&lt;/xs:element&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  &lt;xs:complexType name="c1"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="d"&gt;&lt;/xs:element&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  &lt;xs:complexType name="c2"&gt;
      &lt;xs:sequence&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  </code></pre>