XSD: is there a way to build an enumeration from name/value pairs?

Edward Hamlin picture Edward Hamlin · Jul 16, 2011 · Viewed 8.7k times · Source

I would like to have an enumeration in my XSD that specifies a set of name/value pairs corresponding to error codes and associated descriptions. E.g.:

101  Syntax error
102  Illegal operation
103  Service not available

And so on. I can build a simple structure, event_result, to hold that:

<xs:complexType name="event_result">
    <xs:sequence>
       <xs:element name="errorcode" type="xs:integer"/>
       <xs:element name="errormessage" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

This record would be used in an exception reporting record (as the "result" element):

<xs:complexType name="event_exception">
    <xs:sequence>
        <xs:element name="event_id" type="xs:integer"/>
        <xs:element name="result" type="event_result"/>
        <xs:element name="description" type="xs:string"/>
        <xs:element name="severity" type="xs:integer"/>
    </xs:sequence>
</xs:complexType>

Now the catch is that I would like to define a global enumeration with all the known exception codes and their descriptions. Ideally I would like this to be part of an XSD, not a separate XML data file. I am not sure how to define an enumeration whose members are a complex type - or how to accomplish the same objective in some other way. In a programming language it would a simple two-dimensional array, and it would be easy in XML, but not sure how to do that in an XSD.

Thoughts? Thanks in advance!

Answer

Chris Snow picture Chris Snow · Dec 6, 2012

How about using the xsd:annotation/xsd:appinfo element for holding the error message:

 <xs:simpleType name="event_result">
    <xs:restriction base="xs:string">
      <xs:enumeration value="101">
         <xs:annotation><xs:appinfo>Syntax error</xs:appinfo></xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="102">
         <xs:annotation><xs:appinfo>Illegal operation</xs:appinfo></xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="103">
         <xs:annotation><xs:appinfo>Service not available</xs:appinfo></xs:annotation>
      </xs:enumeration>
    </xs:restriction>
  </xs:simpleType>