title: Creating and using derived types in XML schema
body: <p><strong>Edited in order to make a complete schema that can be tested if needed as suggested in the comments below</strong></p>
  <p>Let's say I have a small XML schema defined as follows:</p>
  <pre><code>&lt;xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
             xmlns:vc=&quot;http://www.w3.org/2007/XMLSchema-versioning&quot;
             targetNamespace=&quot;tns:grades&quot;
             elementFormDefault=&quot;qualified&quot;
             xmlns:tns=&quot;tns:grades&quot;
             vc:minVersion=&quot;1.1&quot;&gt;
  
      &lt;xs:element name=&quot;grades&quot;&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element ref=&quot;tns:grade&quot; minOccurs=&quot;0&quot; maxOcccurs=&quot;unbounded&quot;/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name=&quot;grade&quot;&gt;
          &lt;xs:complexType&gt;
              &lt;xs:simpleContent&gt;
                  &lt;xs:extension base=&quot;xs:double&quot;&gt;
                      &lt;xs:attribute name=&quot;type&quot; type=&quot;tns:gradeType&quot; use=&quot;required&quot;/&gt;
                  &lt;/xs:extension&gt;
              &lt;/xs:simpleContent&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:simpleType name=&quot;gradeType&quot;&gt;
          &lt;xs:restriction base=&quot;xs:string&quot;&gt;
              &lt;xs:pattern value=&quot;simple|complex&quot;/&gt;
          &lt;/xs:restriction&gt;
      &lt;/xs:simpleType&gt;
  
      &lt;xs:simpleType name=&quot;simple&quot;&gt;
          &lt;xs:restriction base=&quot;xs:double&quot;&gt;
              &lt;xs:pattern value=&quot;1.0|1.3|1.7|2.0|2.3|2.7|3.0|3.3|3.7|4.0|4.7|5.0&quot;/&gt;
          &lt;/xs:restriction&gt;
      &lt;/xs:simpleType&gt;
  
      &lt;xs:simpleType name=&quot;complex&quot;&gt;
          &lt;xs:restriction base=&quot;xs:double&quot;&gt;
              &lt;xs:minInclusive value=&quot;1.0&quot;/&gt;
              &lt;xs:maxInclusive value=&quot;5.0&quot;/&gt;
          &lt;/xs:restriction&gt;
      &lt;/xs:simpleType&gt;
  
  &lt;/schema&gt;
  </code></pre>
  <p>The type <em>gradeType</em> is being used to determine how the value of the <strong>grade</strong> element restricted and how it is to be evaluated. What I want to do is use the specified above <em>simple</em> and <em>complex</em> types as two derivatives of <em>gradeType</em>. The <em>simple</em> type of grade allows me to use only a limited set of choices due to a restriction in the grading scale. The <em>complex</em> type on the other hand allows me basically an unlimited interval between 1.0 and 5.0 since I use it to compute the arithmetic mean of multiple <em>simple</em> grades and round it to the closest <em>simple</em> grade available in an XSLT. However I have no idea how to change my <strong>gradeType</strong> in such a way so that both simple types <em>simple</em> and <em>complex</em> can be used when I create a <strong>grade</strong> element. I have read about deriving types (here: <em>simple</em>,<em>complex</em>) from a base class (here: <em>gradeType</em>) but it seems I simply don't get it.</p>
  <p>Example how I imagine <strong>grade</strong> should look like:</p>
  <pre><code>&lt;grades&gt;
      &lt;grade type=&quot;simple&quot;&gt;1.7&lt;/grade&gt;
      &lt;grade type=&quot;simple&quot;&gt;2.0&lt;/grade&gt;
      &lt;grade type=&quot;complex&quot;&gt;1.4&lt;/grade&gt;
      &lt;grade type=&quot;simple&quot;&gt;5.0&lt;/grade&gt;
      &lt;grade type=&quot;complex&quot;&gt;4.6&lt;/grade&gt;
  &lt;/grades&gt;
  </code></pre>
  <p><strong>Edit</strong>
  As suggested below the <strong>xs:union</strong> element was tested:</p>
  <pre><code>&lt;xs:simpleType name=&quot;gradeType&quot;&gt;
      &lt;xs:union memberTypes=&quot;tns:simple tns:complex&quot;/&gt;
  &lt;/xs:simpleType&gt;
  </code></pre>
  <p>This leads to following two errors in all <strong>grade</strong> elements in my XML document:</p>
  <blockquote>
  <p>cvc-datatype-valid.1.2.3: 'simple' is not a valid value of union type 'gradeType'.</p>
  <p>cvc-attribute.3: The value 'simple' of attribute 'type' on element 'grade' is not valid with respect to its type, 'gradeType'.</p>
  </blockquote>
  <p><strong>Edit 2:</strong>
  As noted by @lexicore I have it more or less all wrong or probably just explained badly. I will try to use pseudo-code to describe how the <strong>grade</strong> element is supposed to work:</p>
  <pre><code>if gradeType == &quot;simple&quot;
      value of GRADE has to obey the rules defined by the SIMPLE type
  else if gradeType == &quot;complex&quot;
      value of GRADE has to obey the rules defined by the COMPLEX type
  </code></pre>