简体   繁体   中英

Proper XSD for Enum Type

I have a enum class like,

public enum Test
{
    a = 1,
    b = 2
}

I am creating xsd for these as below

<xs:complexType name="Test">
<xs:all>
    <xs:element name="TCode" type="TestCode" minOccurs="1" maxOccurs="1" />
    <xs:element name="TValue" type="TestValue" minOccurs="1" maxOccurs="1" />
</xs:all>
</xs:complexType>    

<xs:simpleType name="TestCode">
    <xs:restriction base="xs:string">
        <xs:enumeration value="a" />
        <xs:enumeration value="b" />
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="TestValue">
    <xs:restriction base="xs:string">
        <xs:enumeration value="1" />
        <xs:enumeration value="2" />
    </xs:restriction>
</xs:simpleType>
  1. Is this correcty way of creating xsd types for Enum class
  2. How to validate XML element without using xs:assert ?

    if TCode is a then TValue is 1

    if TCode is b then TValue is 2

XML will be,

<Test>
   <TCode>a</TCode>
   <TValue>1</TValue>
</Test>

Any Ideas?

I do not think this is possible using standard xsd functions. But if TCode and TValue have a fixed 1 to 1 relation, should you not better ask just one of those two? This would make it impossible to create an invalid state.

Example:

<xs:complexType name="Test">
 <xs:choise>
  <xs:element name="TCode" type="TestCode" minOccurs="1" maxOccurs="1" />
  <xs:element name="TValue" type="TestValue" minOccurs="1" maxOccurs="1" />
 </xs:choise>
</xs:complexType> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM