3

I'm trying to generate a java classes from multiple xsd files. In these schemas files there are multiple complexType or elements with the same name in different schemas.

Please note that I'm unable to change the schemas.

I created a binding file, but I'm still getting this error

EXAMPLE_QualifyRQ.xsd; ... 'myelement' is already defined

Is there anything I miss in my configuration?

All the schemas are in 1 directory for example:

xsd:

  • EXAMPLE_QualifyRS.xsd
  • EXAMPLE_QualifyRQ.xsd
  • EXAMPLE_PurchaseRQ.xsd
  • EXAMPLE_CommonTypes.xsd
  • EXAMPLE_SimpleTypes.xsd

EXAMPLE_QualifyRS.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00"
 elementFormDefault="qualified" version="1.002" id="EXAMPLE2016.1">
     <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>
     <xs:element name="myelement">...</xs:element>

EXAMPLE_QualifyRQ.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00"
 elementFormDefault="qualified" version="1.002" id="EXAMPLE2016.1">
     <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>
     <xs:element name="myelement">...</xs:element>

EXAMPLE_PurchaseRQ.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00" elementFormDefault="qualified" version="1.001" id="EXAMPLE2016.1">
    <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>
    <xs:include schemaLocation="EXAMPLE_SimpleTypes.xsd"/>
    <xs:element name="myelement">...</xs:element>

And here is how I generate my class in maven

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>example-schema</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <xsdPathWithinArtifact>xsd</xsdPathWithinArtifact>
                            <addGeneratedAnnotation>true</addGeneratedAnnotation>
                            <laxSchemaValidation>true</laxSchemaValidation>
                            <laxSchemaValidation>true</laxSchemaValidation>
                            <readOnly>true</readOnly>
                            <verbose>true</verbose>
                            <sources>
                                <source>src/main/resources/xsd</source>
                            </sources>
                            <xjbSources>
                                <xjbSource>src/main/resources/xjb</xjbSource>
                            </xjbSources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

And my bindings

    <?xml version="1.0" encoding="UTF-8"?>
    <bindings version="2.1"
              xmlns="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              extensionBindingPrefixes="xjc">
        <globalBindings>
            <serializable uid="1"/>
            <xjc:javaType name="java.time.LocalDateTime" xmlType="xs:dateTime" adapter="com.example.adapter.LocalDateTimeAdapter"/>
            <xjc:javaType name="java.time.LocalDate" xmlType="xsd:date" adapter="com.example.adapter.LocalDateAdapter"/>
        </globalBindings>

        <bindings schemaLocation="../xsd/EXAMPLE_QualifyRS.xsd" node="/xs:schema">
            <schemaBindings>
                <package name="com.example.qualify.response"/>
            </schemaBindings>
            <bindings node="//xs:element[@name='myelement']">
                <class name="QualifyMyElement"/>
            </bindings>
        </bindings>

<bindings schemaLocation="../xsd/EXAMPLE_QualifyRQ.xsd" node="/xs:schema">
            <schemaBindings>
                <package name="com.example.qualify.request"/>
            </schemaBindings>
            <bindings node="//xs:element[@name='myelement']">
                <class name="QualifyMyElement"/>
            </bindings>
        </bindings>

        <bindings schemaLocation="../xsd/EXAMPLE_PurchaseRQ.xsd" node="/xs:schema">
            <schemaBindings>
                <package name="com.example.purchase"/>
            </schemaBindings>
            <bindings node="//xs:element[@name='myelement']">
                <class name="PurchaceMyElement"/>
            </bindings>
        </bindings>
    </bindings>
user311633
  • 173
  • 3
  • 11
  • In your bindings you have EXAMPLE_QualifyRS and EXAMPLE_PurchaseRQ. The error is from EXAMPLE_QualifyRQ. Maybe 'myelement' is defined in EXAMPLE_QualifyRQ and another of the rest of your schemas? Add binding for EXAMPLE_QualifyRQ as well, and see if it works or if the message changed. – martidis Oct 25 '18 at 12:12
  • My apologise. I was missing EXAMPLE_QualifyRQ.xsd on my question. I've updated my question above with EXAMPLE_QualifyRQ details. I also tried to bind EXAMPLE_QualifyRS (com.example.qualify.response) to different package than EXAMPLE_QualifyRQ (com.example.qualify.request). My bindings still doesn't seems to work. – user311633 Oct 25 '18 at 21:53
  • The problem is that all your schemas have the same target namespace. – Benoit Oct 25 '18 at 22:27
  • I'm unable to change the schemas though. Is it possible to generate them in group base on file pattern? For example, "*Purchase.xsd" goes to "example.purchase" – user311633 Oct 25 '18 at 23:01
  • Check if in EXAMPLE_CommonTypes.xsd and EXAMPLE_SimpleTypes.xsd they both have a 'myElement'. From what I see in your bindings you do not specify package name for classes generated from this schema. And EXAMPLE_QualifyRQ has both schemas in it. – martidis Oct 26 '18 at 06:09

0 Answers0