xsd

XSD Definition for Enumerated Value


I'm stuck trying to define an XSD containing a field that can have only one of the following three values:

Essentially, I want to define a strict enumeration at the Schema level.

My First attempt appears wrong and I'm not sure about the "right" way to fix it.

<xs:element name="color">
    <xs:complexType>
        <xs:choice>
            <xs:element name="green"/>
            <xs:element name="red"/>
            <xs:element name="blue"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

By using an automatic XML generator, it treats those element names as string objects:

<xs0:color>
    <xs0:green>text</xs0:green>
</xs0:color>

Solution

  • You can define an enumeration within the context of a simpleType.

     <xs:simpleType name="color" final="restriction" >
        <xs:restriction base="xs:string">
            <xs:enumeration value="green" />
            <xs:enumeration value="red" />
            <xs:enumeration value="blue" />
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="SomeElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Color" type="color" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>