I have XML like this:
<type>
<mainType>...</mainType>
<subtype>...<subtype>
</type>
The mainTypes are restricted xs:strings (a list of them). And each mainType has a list of possible subtypes. IE:
mainTypeA has possible subtypes of: subtype1, subtype2, subtype3
mainTypeB has possible subtyes of: subtype4, subtype5
How can I represent this in XSD so that each of subtype enumerations are linked specifically to their main type?
Is there a better way to represent those 2 fields to make the XSD simpler? I am not opposed to changing the XML document structure.
(This would have been a additional comment in @hugh jadick’s answer but was too long for such.)
You have two problems in your initial design which complicates the structure:
<subType>can appear only when<mainType>is present<type>)<mainType>)<subType>elements).In XML Schema 1.0 there is no general method to make those features possible although they can be achieved in some cases and/or with some methods. One possible workaround for ambiguous type problem would be to use
xsi:typeattribute in the instance document to determinate the used schema type (Example 1).Instead of using some possible workarounds to solve those problems, I suggest that you follow @hugh jadick’s answer and create elements with different names for all the main types which again will have different elements for different subtypes. If you don’t like having the type name as the element name (or it is not a valid for an XML element name) you could put it in an attribute, possibly using a default or fixed value (
<myType1 typeName="Type 1 name">)(Example 2). If subtypes are mutually exclusive, you could put also them in an attribute (<myType1 subType="subType2">). If you really want to have the type name in the element contents (<mainType1>Type 1 name</mainType1>), then it is probably better to have the<subType>elements as following siblings instead of children of<mainType1>because that would result in mixed content which easily causes whitespace and text node position related problems (Example 3).Yes, substitution groups could also be used with @hugh jadick’s answer. You would have an abstract
<mainType>element and all main type element definitions hadsubstitutionGroup="mainType"attribute. You would still need to have different names for different main type elements because they allow different set of allowed elements/values. The type of these elements must be derived from the type of the abstract<mainType>element that they substitute (Example 4).Code examples
Example 1
Example 2
Example 3
Example 4