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
Could it be that you just want the union datatype?
(Not tested.)
This should allow you to use values of
tns:simpleortns:complextypes astns:gradeType.See the relevant examples from the here (can't copy as not sure about the license).
Also consider using
xs:enumerationintns:simpleinstead ofxs:pattern.