I have several XSD files containing structures like the following:
<xs:complexType name="SomeThing" abstract="false">
<xs:sequence>
<xs:element name="id" type="schema2:SomeIdTypeClass" minOccurs="1" maxOccurs="1"/>
<xs:element name="myType" type="schema1:MyType" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="MyType">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="APPLE"/>
<xs:enumeration value="ORANGE"/>
<xs:enumeration value="BANANA"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="OTHER:[a-zA-Z_][a-zA-Z0-9_]*"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
I am using the XJC command to generate the Java classes on the console, not maven or ant or any other build tool.
All Classes seem to be generated fine except these kind of enums. The Classes that are using the enum Type are only referecning it as String, e.g. the Something class contains MyType only as a String, and not as an ENUM, as I would expect it.
In another file I have something like this, where the Enums are generated without problems and referencing classes are using it as Enums as they should.
<xs:simpleType name="SizeType">
<xs:restriction base="xs:string">
<xs:enumeration value="SMALL"/>
<xs:enumeration value="MEDIUM"/>
<xs:enumeration value="LARGE"/>
</xs:restriction>
I do not want to alternate the XSD files since they are not generated by myself and I have a few files with many of those elements.
I came to the conclusion that i need to use some kind of XJB binding file, but I cannot figure out, what exactly i need to write there:
The following gives me only the error "results in too many target nodes"
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:bindings schemaLocation="MySchema.xsd" node="/xs:schema">
<jaxb:bindings node="//xs:simpleType[@name='MyType']/xs:union/xs:simpleType">
<jaxb:typesafeEnumClass name="MyType"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Adding multiple="true" solves that but still does not give me the Enums that I want.
Edit: When i remove the xs:union so that i have a normal xs:simpleType like this:
<xs:simpleType name="MyType">
<xs:restriction base="xs:string">
<xs:pattern value="OTHER:[a-zA-Z_][a-zA-Z0-9_]*"/>
<xs:enumeration value="APPLE"/>
<xs:enumeration value="ORANGE"/>
<xs:enumeration value="BANANA"/>
</xs:restriction>
</xs:simpleType>
Then it works as intended. I still dont want to go down that route and edit the schemas if I dont have to. There must be some way around.