JAXB - Property "Value" is already defined. Use <jaxb:property> to resolve this conflict
Using JAXB to generate XML binding classes.
The schema is based on a set of legacy XML files, and includes this snippet:
<xs:complexType name="MetaType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="Name" />
<xs:attribute type="xs:string" name="Scheme" />
<xs:attribute type="xs:string" name="Value" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
The 'Value' attribute conflicts with the 'value' property of xs:string
, and the code generation fails with the error:
com.sun.istack.SAXParseException2: Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
Solution 1:
The answer lies in making use of JAXB bindings (site-template.xjb
):
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="site-template.xsd" version="1.0">
<!-- Customise the package name -->
<schemaBindings>
<package name="com.example.schema"/>
</schemaBindings>
<!-- rename the value element -->
<bindings node="//xs:complexType[@name='MetaType']">
<bindings node=".//xs:attribute[@name='Value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
The XPath expressions locate the nodes and renames it, thereby avoiding the naming conflict.
Using this bindings XML file, the generated Java class ends up having the desired getValueAttribute()
(as well as the getValue()
).
Solution 2:
If you want to avoid creating/changing a JAXB bindings file, and you don't mind annotating your XSD, you can add the jxb:property annotation to your attribute's definition, e.g.:
<xs:complexType name="MetaType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="Name" />
<xs:attribute type="xs:string" name="Scheme" />
<xs:attribute type="xs:string" name="Value">
<!-- rename property generated by JAXB (avoiding "Value" name conflict) -->
<xs:annotation>
<xs:appinfo>
<jxb:property name="valueAttribute"/>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
with suitable additions to the xs:schema tag:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.1">