0

I have the following snippet in my XSD file. I would expect that it generates a Java enum with the two elements STANDALONE and CONNECTED.

<xs:attribute name="Sample">
  <xs:annotation>
    <xs:documentation>A sample</xs:documentation>
  </xs:annotation>
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Standalone"/>
      <xs:enumeration value="Connected"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>

I'm using jaxb2-maven-plugin version 3.1.0

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>gen-infrastructure</id>
      <goals>
        <goal>xjc</goal> <!-- Generates Java sources from XML Schema(s). -->
      </goals>
      <configuration>
        <locale>en,US</locale>
        <xjbSources>
          <xjbSource>src/main/resources/schema/xsd/sample.xjb</xjbSource>
        </xjbSources>
        <sources>
          <source>src/main/resources/schema/xsd/sample.xsd</source>
        </sources>
        <!-- The package of your generated sources -->
        <packageName>com.company.sample.model.pojos</packageName>
      </configuration>
    </execution>
  </executions>
  <configuration>
    <noGeneratedHeaderComments>true</noGeneratedHeaderComments>
    <clearOutputDir>false</clearOutputDir>
  </configuration>
</plugin>

I looked for the same problem on GitHub. I asked another employee in my company if they had a solution or saw what is wrong.

1 Answer 1

0

Try

<xs:attribute name="Sample" type="MyEnum">
  <xs:annotation>
    <xs:documentation>A sample</xs:documentation>
  </xs:annotation>
</xs:attribute>

<xs:simpleType name="MyEnum">
   <xs:restriction base="xs:string">
     <xs:enumeration value="Standalone"/>
     <xs:enumeration value="Connected"/>
   </xs:restriction>
 </xs:simpleType>

I don't known whether you use namespaces. Maybe you have to add one to the type attribute within the attribute definition.

Edit

If the schema is fixed you can use a workaround with JAX Binding Customization:

Add the following to your sample.xjb

    <jxb:bindings schemaLocation="sample.xsd">
        <jxb:bindings node="xs:attribute[@name='Sample']/xs:simpleType">
            <jxb:typesafeEnumClass name="Sample"/>
        </jxb:bindings>
    </jxb:bindings>

As I neither know about your namespaces nor the exact position of the attribute within the schema you'll have to add your namespace declaration to the bindings file and adopt the XPath statement of the node attribute.

Sign up to request clarification or add additional context in comments.

8 Comments

I can't change the XSD File. Its given and fix.
I found an even better solution than the last (now deleted) one.
In my opinion these enums have to be generated.
This is my opinion, too. The current version does this. But sometimes there is no other possibility and then a workaround is better than nothing.
I'm using 3.1.0 (the current version)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.