This type of problem often occurs when a type has an attribute and element with the same name.
schema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema"
xmlns:tns="http://www.example.org/schema"
elementFormDefault="qualified">
<element name="foo">
<complexType>
<sequence>
<element name="bar" type="string"/>
</sequence>
<attribute name="bar" type="string"/>
</complexType>
</element>
</schema>
xjc schema.xsd
When we try to generate a Java model from this XML schema we get the following error.
parsing a schema...
[ERROR] Property "Bar" is already defined. Use <jaxb:property> to resolve this conflict.
line 11 of file:/Users/bdoughan/Scratch/src/forum16714465/schema.xsd
[ERROR] The following location is relevant to the above error
line 13 of file:/Users/bdoughan/Scratch/src/forum16714465/schema.xsd
binding.xml
A JAXB binding file can be used to customize the classes that are generated. Here we will use it to rename the property that will correspond to the bar
attribute.
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="//xs:element[@name='foo']/xs:complexType/xs:attribute[@name='bar']">
<jxb:property name="barAttribute"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
xjc -b binding.xml schema.xsd
Now when you generate the Java classes you will get a class like:
package org.example.schema;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"bar"
})
@XmlRootElement(name = "foo")
public class Foo {
@XmlElement(required = true)
protected String bar;
@XmlAttribute(name = "bar")
protected String barAttribute;
public String getBar() {
return bar;
}
public void setBar(String value) {
this.bar = value;
}
public String getBarAttribute() {
return barAttribute;
}
public void setBarAttribute(String value) {
this.barAttribute = value;
}
}