tags:

views:

1129

answers:

1

Following is the sample schema I used to try to generate the JAXB classes. What I noticed is that when I have a string type with enumerations, for instance, in my case the stepType, with values starting with a numeral, JAXB does not generate a separate enum class say StepType class. It works fine, when I use only alphabets for the value. Could someone tell whether this is a known bug please?

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema"&gt;

<xsd:element name="WorkoutSchedule" >
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="WorkoutSchedule" minOccurs = "0" maxOccurs="unbounded">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="ScheduleItem" minOccurs="1" maxOccurs="1">
          <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Step" minOccurs="1" maxOccurs="1" type="stepType"/>
          </xsd:sequence>
          </xsd:complexType>
          </xsd:element>
        </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

<xsd:simpleType name="stepType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="1stStep"></xsd:enumeration>
    <xsd:enumeration value="2ndStep"></xsd:enumeration>
    <xsd:enumeration value="3rdStep"></xsd:enumeration>
  </xsd:restriction>
</xsd:simpleType>

</xsd:schema>
+2  A: 

JAXB have to create valid Java code, so it's bound to Java-specific naming rules.

Java's enums are required to have Java-specific naming, which is equal for classes, methods, fields, ... No name is allowed to begin with a numerical digit.

You can marshal existing Java enums using JAXB to XML schema with custom names by overriding them using

/* some JAXB annotations go here */
enum MyEnum{
    @XmlEnumValue(name="1")
    ONE,
    @XmlEnumValue(name="2")
    TWO
}

The opposite process is done with a little 'workaround':

<xsd:simpleType>
 <xsd:annotation>
  <xsd:appinfo>
  <jxb:typesafeEnumClass name="MyEnum">
   <jxb:typesafeEnumMember name="ONE" value="1"/>
   <jxb:typesafeEnumMember name="TWO" value="2"/>
  </jxb:typesafeEnumClass>
  </xsd:appinfo>
 </xsd:annotation>
 <xsd:restriction base="xsd:int">
  <xsd:enumeration value="1"/>
  <xsd:enumeration value="2"/>
 </xsd:restriction>
</xsd:simpleType>
ivan_ivanovich_ivanoff