XSD:有没有办法从名称/值对构建枚举?



我希望在XSD中有一个枚举,它指定一组对应于错误代码和相关描述的名称/值对。例如:

101  Syntax error
102  Illegal operation
103  Service not available

以此类推。我可以构建一个简单的结构event_result来保存它:

<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>

此记录将用于异常报告记录(作为"result"元素):

<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>

现在的问题是,我想定义一个包含所有已知异常代码及其描述的全局枚举。理想情况下,我希望它是XSD的一部分,而不是单独的XML数据文件。我不知道如何定义成员为复杂类型的枚举,也不知道如何以其他方式实现相同的目标。在编程语言中,它是一个简单的二维数组,在XML中很容易,但不确定如何在XSD中做到这一点。

想法吗?提前感谢!

如何使用xsd:annotation/xsd:appinfo元素来保存错误消息:

 <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>

我认为xsd本身不支持您想要的东西。我见过这样的实现:

  <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>

最新更新