Search code examples
javaxsdjaxb

How to handle elements & attributes having same name in xsd while generating pojos using jaxb?


I have a xsd that contains something like:

<xs:complexType>
  <xs:sequence minOccurs="0">
    <xs:element ref="HereIsTheProblem"/>
    <xs:element ref="blaBla"/>
  </xs:sequence>
  <xs:attribute name="something" type="xs:string" use="required">
    <xs:annotation/>
  </xs:attribute>
  <xs:attribute name="somethingElse" type="xs:string">
    <xs:annotation/>
  </xs:attribute>
  <xs:attribute name="HereIsTheProblem" type="xs:string">
    <xs:annotation/>
  </xs:attribute>
</xs:complexType>

now when i try to parse the schema using jaxb to generate java classes it fails:

[ERROR] Element "{http://something.somemorething.com/etc/}HereIsTheProblem" shows up in more than one properties.

how to resolve this without making any modification in the schema?

PS:my jaxb version is 2.1.13


Solution

  • You need to use a binding file indicating jaxB how it should handle this name collision. For example, put something like this in a file named something like bindings.xjb:

    <jaxb:bindings version="2.1" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:com.fnf="http://www.fnf.com/xes">
      <jaxb:bindings schemaLocation="your schema location here" node="/xs:schema">
        <jaxb:bindings node="//XPath selector">
          <jaxb:property name="HereIsTheProblem2" />
        </jaxb:bindings>
      </jaxb:bindings>
    </jaxb:bindings>
    

    Can't provide you a complete solution without a complete schema