Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD Definition for Enumerated Value

Tags:

xsd

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

  • Green
  • Red
  • Blue

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>
like image 470
Nate Avatar asked Jul 22 '09 21:07

Nate


People also ask

What is enumeration value in XSD?

Enumerations are a base simple type in the XSD specification containing a list of possible values. Single-valued enumerations are shown as restrictions of the base simple type xs:token , as illustrated below: ? < xs:simpleType name=”GraduationPlanTypeMapType”> . . .

How can we defined within an XSD?

Defining XML ElementsEach element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.

How is length defined in XSD?

you can always define the maximal length of a string in xsd. Just add the attribute maxLength resp. minLength .


2 Answers

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>
like image 191
Colin Cochrane Avatar answered Oct 18 '22 09:10

Colin Cochrane


This solution worked for me:

<xs:element name="color">
   <xs:simpleType>
      <xs:restriction base="xs:string">
          <xs:enumeration value="green"/>
          <xs:enumeration value="red"/>
          <xs:enumeration value="blue"/>
      </xs:restriction>
   </xs:simpleType>
</xs:element>
like image 21
salerokada Avatar answered Oct 18 '22 09:10

salerokada