4

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?

Oleg Kandaurov
  • 316
  • 1
  • 6
  • 12
  • Can you post your plugin configuration from your `pom.xml`? I cannot find (reach) the documentation for the `maven-jaxb2-plugin` right now and I don't know all the options by heart. If it's feasible try adding a `targetNamespace="http://whatever"` attribute to your schemas to differentiate types with the same names. It is possible to compile every schema file into a different Java package. That could work also. For this to work you probably have to specify multipe [executions](http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag) for the plugin. – Kohányi Róbert Dec 29 '11 at 08:29

3 Answers3

6

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?).

Community
  • 1
  • 1
Andrew B
  • 1,618
  • 2
  • 21
  • 30
1

you can rename the second or the first A3 of your xsd in your jaxb binding file

<jaxb:bindings schemaLocation="filePath.xsd" node="/xs:schema">
        <jaxb:bindings node="//xs:element[@name='A3']">
            <jaxb:property name="SecondA3"/>
        </jaxb:bindings>
</jaxb:bindings>
Kaushik
  • 3,371
  • 3
  • 23
  • 32
arash
  • 176
  • 1
  • 5
1

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.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327