'How to rename xs:simpleType during jaxb XJC generation?

I'm using jaxb and xjc to autogenerate java classes from xsd files.

Problem: I have two files that show the same simpleType name. And thus get a compilation error:

xsd1.xsd [49:3]: 'FileKey' is already defined

xsd1:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:simpleType name="FileKey">
  <xs:restriction base="xs:string">
   <xs:pattern value="[A-Z0-9_]+" />
  </xs:restriction>
 </xs:simpleType>
 ...
</xs:schema>

xsd2:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:simpleType name="FileKey">
  <xs:restriction base="xs:string">
   <xs:pattern value="[A-Z0-9_]+" />
  </xs:restriction>
 </xs:simpleType>
 ...
</xs:schema>

My binding file that I tried:

<jaxb:bindings
        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:version="2.1">

    <jaxb:bindings schemaLocation="xsd1.xsd">
        <jaxb:bindings node="//xs:simpleType[@name='FileKey']">
            <jaxb:class name="FileKeyRenamed" />
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

Sidenote: I cannot modify the xsds, as I have no conrol of them.



Solution 1:[1]

Try using typeSafeEnumClass annotation:

<jaxb:bindings schemaLocation="xsd1.xsd">
    <jaxb:bindings node="//xs:simpleType[@name='FileKey']" >
        <jaxb:typesafeEnumClass name="FileKeyRenamed" />
    </jaxb:bindings>
</jaxb:bindings>

See more here: https://coderleaf.wordpress.com/2016/11/15/jaxb-bindings-by-example/.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tharsus