I have an XSD that works today for validating a special kind of element we have of type "Option". As an example, here is the current definition of an element called "operation":
<xsd:element name="operation" type="Option" minOccurs="0"/>
And here is the definition of type Option:
<xsd:complexType name="Option">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="option" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
So this element might be left out or might appear once, and it typically looks something like this:
Insert
I would like to make our XSD more strict while at the same time including information for vendors who get the XSD to enumerate the possible values for the option attribute, the possible (related/translated) values for the element itself, and what default is assumed if the element is missing entirely. So if the only two values for the option attribute are "I" and "U" and the actual element value should be either "Insert" or "Update" (or blank, actually), and the default value if the element is missing entirely is "U"/Update, I would like to write my XSD for this element to include all that information and to successfully validate any of the following four options:
<operation option="U"/>
<operation option="I"/>
<operation option="U">Update</operation>
<operation option="I">Insert</operation>
It should also successfully validate the document if this element is missing entirely. It should NOT validate successfully if the element is present but the option attribute is missing, if it is anything other than "U" or "I". It also shouldn't validate successfully if the element is present and the text value for the element is present but is anything other than "Update" or "Insert". Furthermore, it shouldn't validate if the option attribute is "U" but the text for the element reads "Insert" or vice versa ("I" and "Update"). As if all that isn't bad enough, I'm hoping to avoid defining new types, because I am going to be doing this for several data types of type Option, and all of these value lists aren't global--I'm going to want to include them as part of the each element definition if possible.
So far, I have only made it as far as making the "option" attribute be required and be an enumeration:
<xsd:element name="operation" minOccurs="0">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="option" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="I"/>
<xsd:enumeration value="U"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
I can't specify a default value for the option attribute when I make the attribute required. But my need for a default is to specify that if the element is missing entirely, it should be treated as though Update were in the document.
Any XSD experts out there that can suggest the best way to meet all these goals? If I can't do it without defining new types by name outside of the actual element definition, I suppose I can try to do that, but it will be harder to avoid name collisions and the enumeration information would end up too far from the element definition itself, so I'd like to avoid that need if I can.