tags:

views:

198

answers:

3

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

  • Green
  • Red
  • Blue

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

A: 

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>
Nate
+4  A: 

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>
Colin Cochrane
I would recommend using an extension of NMTOKEN, though, rather than String. It's more consist with the idea of an enum, I think. It's also more tool-friendly, particularly with code generators.
skaffman
A: 

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

text

This should work.I used it and it is working fine.

Regards, Hari Mohan