3

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!

Edward Hamlin
  • 101
  • 1
  • 3

2 Answers2

4

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>
Oxymoron
  • 1,380
  • 2
  • 19
  • 47
Chris Snow
  • 23,813
  • 35
  • 144
  • 309
  • Is xs*d* a typo or intentional? Someone suggested an edit that changed from xs to xsd but I can't tell if that's a mistake. – SuperBiasedMan Jul 31 '15 at 16:13
  • I suggested the edit, Visual Studio 2013 prompted that it was an error. See this post: http://stackoverflow.com/questions/7787481/xsd-different-between-xsdelement-and-xselement – Oxymoron Aug 01 '15 at 19:59
  • In general it doesn't matter and has something to do with your xml declarations. So I suggested the edit, since everything else in this thread uses "xs" and not "xsd". – Oxymoron Aug 01 '15 at 20:01
2

I don't think xsd supports what you want natively. I've seen implementations like this:

  <xs:simpleType name="event_result">
    <xs:restriction base="xs:string">
      <xs:enumeration value="101, Syntax error"/>
      <xs:enumeration value="102, Illegal operation"/>
      <xs:enumeration value="103, Service not available"/>
    </xs:restriction>
  </xs:simpleType>
Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
  • Thanks, Otavio. That is what I will do if there's no better way...I was just hoping there was. ;) – Edward Hamlin Jul 15 '11 at 23:03
  • XSD's can define restrictions on individual elements but there is no support (that I know of) for restrictions across elements, and I guess that is outside the scope. However, you may be able to "fake" that by establishing a convention and creating separate restrictions that are positionally correspondent so that you can use the same index to retrieve the key and the value. It just doesn't look kosher. – Otávio Décio Jul 15 '11 at 23:10
  • Since the actual contents of an entry would be processed in (Ruby or C#) code anyway, I was probably just going to use a simple convention like code|description with a pipe-sign delimiter. Ruby or C# can easily pull that apart with string extract functions. I don't really need the XML to understand the semantics. So it's really not THAT bad of an approach... – Edward Hamlin Jul 15 '11 at 23:22
  • I agree and you may even be able to create a restriction with a pattern (regex) establishing the code|description convention. – Otávio Décio Jul 15 '11 at 23:24