Edited in order to make a complete schema that can be tested if needed as suggested in the comments below
Let's say I have a small XML schema defined as follows:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
targetNamespace="tns:grades"
elementFormDefault="qualified"
xmlns:tns="tns:grades"
vc:minVersion="1.1">
<xs:element name="grades">
<xs:complexType>
<xs:sequence>
<xs:element ref="tns:grade" minOccurs="0" maxOcccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="grade">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:double">
<xs:attribute name="type" type="tns:gradeType" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name="gradeType">
<xs:restriction base="xs:string">
<xs:pattern value="simple|complex"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="simple">
<xs:restriction base="xs:double">
<xs:pattern value="1.0|1.3|1.7|2.0|2.3|2.7|3.0|3.3|3.7|4.0|4.7|5.0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="complex">
<xs:restriction base="xs:double">
<xs:minInclusive value="1.0"/>
<xs:maxInclusive value="5.0"/>
</xs:restriction>
</xs:simpleType>
</schema>
The type gradeType is being used to determine how the value of the grade element restricted and how it is to be evaluated. What I want to do is use the specified above simple and complex types as two derivatives of gradeType. The simple type of grade allows me to use only a limited set of choices due to a restriction in the grading scale. The complex 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 simple grades and round it to the closest simple grade available in an XSLT. However I have no idea how to change my gradeType in such a way so that both simple types simple and complex can be used when I create a grade element. I have read about deriving types (here: simple,complex) from a base class (here: gradeType) but it seems I simply don't get it.
Example how I imagine grade should look like:
<grades>
<grade type="simple">1.7</grade>
<grade type="simple">2.0</grade>
<grade type="complex">1.4</grade>
<grade type="simple">5.0</grade>
<grade type="complex">4.6</grade>
</grades>
Edit As suggested below the xs:union element was tested:
<xs:simpleType name="gradeType">
<xs:union memberTypes="tns:simple tns:complex"/>
</xs:simpleType>
This leads to following two errors in all grade elements in my XML document:
cvc-datatype-valid.1.2.3: 'simple' is not a valid value of union type 'gradeType'.
cvc-attribute.3: The value 'simple' of attribute 'type' on element 'grade' is not valid with respect to its type, 'gradeType'.
Edit 2: 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 grade element is supposed to work:
if gradeType == "simple"
value of GRADE has to obey the rules defined by the SIMPLE type
else if gradeType == "complex"
value of GRADE has to obey the rules defined by the COMPLEX type
After long hours of struggling I came up with the following and it is doing what I want it to be doing with 2 exceptions:
Those problems come most probably from the fact that my grade attributes come actually with my base type gradeType.
As you can see below I have reworked my XSD concerning the part handling the grade element. I have added some of the additional attributes that I needed (haven't described @term and @attempt in my question). You can see the XSD code below after the explanation that follows.
I started with some brute-force techniques experimenting what sort of combinations I can make with xs:complexType and xs:simpleType because my idea was actually circling around those two. Then I started looking at alternatives of describing the same element in multiple ways. The keyword alternatives gave me an idea to look for such an element and indeed xs:alternative is the solution in my case. The major breakthrough was when I remembered that an element can have 4 types of content: empty (that is no content at all), text, another element or combined. The main issue was that all the examples I managed to find concerning xs:alternative were with alternatives for a nested structure of child elements of the current element that they were part of. I decide to test it with text content and it actually worked. So here is how it works: you create a complexType gradeType, which is used for two things - describes the attributes of my grade element and is a base for the two types of grades I want to have - simple and complex. It is also defined as an extension of the simpleType xs:double. All three have in common the fact that they are of complexType and with simpleContent the second being required when you want to use xs:restriction and create a container for a simpleType (xs:double).
So that's it. The only thing that remains is for me to figure out how to include my attributes in the grade element and not in the gradeType since this leads (for some unknown for me at least reason) to the problems I described at the beginning of my own answer.