Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating JAXB class from XSDs with similar attribute names

I use maven-jaxb2-plugin to generate jaxb annotated classes from xsd. I have many xsd files like those:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="A3">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="loginPartner">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="login"/>
              <xs:element type="xs:string" name="password"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="A3">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="errorCode"/>
        <xs:element type="xs:string" name="errorDescription"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

When I run maven plugin it gives me an error:

[ERROR] Error while parsing schema(s).Location [ file:schema1.xsd{10,16}]. org.xml.sax.SAXParseException: 'A3' is already defined

Is there any way to fix this? Actually I have many XSDs representing request/response messages to/from server. I want to simplify creating, validating, parsing messages. Maybe is there another solution for this?

like image 484
Oleg Kandaurov Avatar asked Feb 22 '23 00:02

Oleg Kandaurov


2 Answers

I had a similar problem; I had two separate and independent WSDL (each with several schema definitions in each) that I was running through JAXB (via the maven-jaxb2-plugin) to generate mapping classes.

My WSDL's shared a duplicate schema definition that was causing XJC to choke.

Because they were both independent, I was able to generate JAXB mappings by running two separate executions of the maven-jaxb2-plugin - one for each WSDL (covered here - How can I tell jaxb / Maven to generate multiple schema packages?).

like image 129
Andrew B Avatar answered Mar 26 '23 10:03

Andrew B


You cannot have conflicting element definitions within the same namespace. This is same as not allowing multiple classes with the same name in a given package in Java. Your best bet is to define them with different names or in different namespaces.

like image 34
Aravind Yarram Avatar answered Mar 26 '23 10:03

Aravind Yarram