title: Extend complex types from an existing schema
body: <p>I need to extend an existing XML document with new elements without losing the ability to validate it against both the unchanged original schema and an extended schema defined by me.</p>
  
  <p>For example, lets say I want to add <code>Code</code> and <code>Price</code> elements to an existing <code>Book</code> document, as follows:</p>
  
  <pre><code>&lt;aa:Book xmlns:aa="http://www.aa.com"
           xmlns:bb="http://www.bb.com"&gt;
    &lt;aa:Title&gt;Complete Works&lt;/aa:Title&gt;
    &lt;aa:Author&gt;Shakespeare&lt;/aa:Author&gt;
    &lt;bb:Code&gt;98&lt;/bb:Code&gt;
    &lt;bb:Price&gt;31.6&lt;/bb:Price&gt;
  &lt;/aa:Book&gt;
  </code></pre>
  
  <p>The original schema (which I cannot change) would look something like this:</p>
  
  <pre><code>&lt;xs:schema targetNamespace="http://www.aa.com"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns="http://www.aa.com"
             elementFormDefault="qualified"&gt;
    &lt;xs:element name="Book" type="Book--type" /&gt;
    &lt;xs:complexType name="Book--type"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="Title" type="xs:string" /&gt;
        &lt;xs:element name="Author" type="xs:string" /&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>How would I create a new schema that defines these new elements such that a <code>SchemaSet</code> containing both these schemas can successfully validate the extended XML document above?</p>
  
  <p>I've tried the following usual complex type extension pattern:</p>
  
  <pre><code>&lt;xs:schema targetNamespace="http://www.bb.com"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns="http://www.bb.com"
             xmlns:aa="http://www.aa.com"
             elementFormDefault="qualified"&gt;
    &lt;xs:import namespace="http://www.aa.com" /&gt;
    &lt;xs:complexType name="Book--type"&gt;
      &lt;xs:complexContent&gt;
        &lt;xs:extension base="aa:Book--type"&gt;
          &lt;xs:sequence&gt;
            &lt;xs:element name="Code" type="xs:int" /&gt;
            &lt;xs:element name="Price" type="xs:double" /&gt;
          &lt;/xs:sequence&gt;
        &lt;/xs:extension&gt;
      &lt;/xs:complexContent&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>However this causes errors stating that the element <code>aa:Book</code> doesn't define a child element <code>bb:Code</code>, which makes total sense.</p>
  
  <p>Is this even possible?</p>
  
  <p>For reference, here is the code I'm using to validate the document:</p>
  
  <pre><code>var fail = new ValidationEventHandler((sender, e) =&gt; Debug.Fail(e.Message));
  var schemaSet = new XmlSchemaSet();
  schemaSet.Add(XmlSchema.Read(new StringReader(original), fail));
  schemaSet.Add(XmlSchema.Read(new StringReader(extension), fail));
  var settings = new XmlReaderSettings
      {
          ValidationType = ValidationType.Schema,
          Schemas = schemaSet
      };
  settings.ValidationEventHandler += fail;
  using (var stream = File.OpenRead(fileName))
  using (var reader = XmlReader.Create(stream, settings))
  {
      while (reader.Read()) {}
  }
  </code></pre>