Xsd with Enumeration - Getting error while puting enumeration type

38 views Asked by At

I am tryin to add Enumeration type as you can see below. it adds string type but i need enum Values when jaxb converts xsd to java class. I used simpletype which converts it into string but not sure how to change it inside element as if I try to change it it gives me error.

<xs:element name="DeletionReq">
    <xs:complexType>
        <xs:sequence>
                <xs:element name="Id" type="xs:long"/>
                <xs:element name="isNewProcess" type="xs:boolean" default="false"/>
                <xs:element name="data" type="tns:data" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="data">
    <xs:sequence>
                      <xs:element name="dataColumnName" type="xs:string" minOccurs="0" />
                      <xs:element name="dataOldValue"   type="xs:string" minOccurs="0"/>
                      <xs:element name="dataNewValue"   type="xs:string" minOccurs="0"/>
                      <xs:element name="testRecord"   type="xs:string" minOccurs="0"/>
                      <xs:element name="projId"   type="xs:string" minOccurs="0"/>
                      <xs:element minOccurs="0" name="allowedApplDataColumnNames">
                          <xs:simpleType>
                            <xs:restriction base="xs:string">
                              <xs:enumeration value="code1" />
                              <xs:enumeration value="code2" />
                              <xs:enumeration value="code3" />
                              <xs:enumeration value="code4" />
                              <xs:enumeration value="code5" />
                            </xs:restriction>
                          </xs:simpleType>
                       </xs:element>
    </xs:sequence>
</xs:complexType>
2

There are 2 answers

3
Karsten On BEST ANSWER

Make an explicit type from your enum:

    <xs:simpleType name="myEnum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="code1" />
            <xs:enumeration value="code2" />
            <xs:enumeration value="code3" />
            <xs:enumeration value="code4" />
            <xs:enumeration value="code5" />
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="data">
        <xs:sequence>
            ...
            <xs:element minOccurs="0" name="allowedApplDataColumnNames" type="tns:myEnum"/>
        </xs:sequence>
    </xs:complexType>
3
Laurent Schoelens On

You can define the type outside of the complexType data like the following :

<xs:simpleType name="allowedApplDataColumnNamesType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="code1" />
        <xs:enumeration value="code2" />
        <xs:enumeration value="code3" />
        <xs:enumeration value="code4" />
        <xs:enumeration value="code5" />
    </xs:restriction>
</xs:simpleType>

<xs:complexType name="data">
    <xs:sequence>
        <xs:element name="dataColumnName" type="xs:string" minOccurs="0" />
        <xs:element name="dataOldValue" type="xs:string" minOccurs="0"/>
        <xs:element name="dataNewValue" type="xs:string" minOccurs="0"/>
        <xs:element name="testRecord" type="xs:string" minOccurs="0"/>
        <xs:element name="projId" type="xs:string" minOccurs="0"/>
        <xs:element name="allowedApplDataColumnNames" type="tns:allowedApplDataColumnNamesType" minOccurs="0" />
    </xs:sequence>
</xs:complexType>