XSD1.1 allows the type of an element to depend on one of its attributes. For example,
<integer kind='s'>100</integer>
will cause the type of 'element' to be xs:short. Here is what I have got:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
vc:minVersion="1.1">
<xs:complexType name="GenericInt">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="kind" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="integer" type="GenericInt">
<xs:alternative test="@kind='b'" type="xs:byte"/>
<xs:alternative test="@kind='s'" type="xs:short"/>
<xs:alternative test="@kind='i'" type="xs:int"/>
<xs:alternative test="@kind='l'" type="xs:long"/>
<xs:alternative type="GenericInt"/>
</xs:element>
</xs:schema>
When I tried to save the file in Altova XMLSpy, an error occurred: cos-st-derived-ok.2: Simple type definition 'xs:byte' is not validly derived from 'GenericInt'.
So how should I correct the XSD code?
Well, I got it working like this:
Any better solution?
An alternative approach would be something like
though you wouldn't get the precise PSVI type annotation that way.