0

I found a similar discussion on this topic here. But the scenario there was completely different from this one and the solution does not work for me. So I am bringing this question again.

My XSD (sample.xsd)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="field">
      <xsd:complexType>
         <xsd:choice maxOccurs="unbounded" minOccurs="0">
            <xsd:element maxOccurs="unbounded" minOccurs="0" name="ProgramLevel">
               <xsd:complexType>
                  <xsd:attribute name="value" type="xsd:string" use="optional"/>
                  <xsd:attribute name="desc" type="xsd:string" use="optional"/>
               </xsd:complexType>
            </xsd:element>
            <xsd:element maxOccurs="unbounded" minOccurs="0" name="Program">
               <xsd:complexType>
                  <xsd:sequence maxOccurs="unbounded" minOccurs="0">
                     <xsd:element maxOccurs="unbounded" minOccurs="0" name="Level">
                        <xsd:complexType>
                           <xsd:attribute name="value" type="xsd:string" use="optional"/>
                           <xsd:attribute name="desc" type="xsd:string" use="optional"/>
                        </xsd:complexType>
                     </xsd:element>
                  </xsd:sequence>
               </xsd:complexType>
            </xsd:element>
         </xsd:choice>
      </xsd:complexType>
    </xsd:element>
</xsd:schema>

My XML

<field>
    <ProgramLevel value="x" />
</field>
<field>
    <Program>
        <Level value="y" />
    </Program>
</field>

Following error coming on running xjc command

[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 7 of file:/D:/ProgramPractice/CreateXSD/JAXB/sample.xsd

[ERROR] (Related to above error) This is the other declaration.
  line 16 of file:/D:/ProgramPractice/CreateXSD/JAXB/sample.xsd

Failed to produce code.

Any idea how to solve the conflict between 'ProgramLevel' and 'Program->Level' with the help of binding file? Thanks in advance.

3 Answers3

0

That's very simple. You are using the choice composer in the definition of the field element's type. But choice means that among all particles enclosed in it, only single one may appear in the destination XML. So, according to your sample.xsd schema, <ProgramLevel> and <Program> may not appears simultaneously as children of <field> element. Only one of them may be used!

If you want both, you should use xsd:sequence composer instead and modify your schema as follows:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="field">
    <xsd:complexType>
       <xsd:sequence maxOccurs="unbounded" minOccurs="0">
       ...
       </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

Then, your XML will pass the validation (without collisions).

Note that xsd:sequence composer also has its restrictions. It implies that the particles specified in it should appear in the destination XML strictly in the same order as in the XML schema. That is, the following will be also wrong: <field><Program>...</Program><ProgramLevel/></field>. So, you should modify the schema accordingly, if you need that. However, if the ordering must not be restricted at all, use all composer (or even more complicated construction).

P.S. The following links may be interesting for those working with XML schemas and WSDL:

If you value my participation on this site, please do not remove these links!

ColdFusion
  • 2,251
  • 10
  • 13
  • Thanks for your response. I edited the XML as only one type of particle can come under field tag. That was a typo earlier. I have to use choice in my case.. – Abhishek Mukherjee Jan 31 '19 at 05:56
0

The issue is finally resolved by adding following to the binding file:

<jxb:bindings schemaLocation="sample.xsd" node="/xsd:schema">
    <jxb:bindings node="//xsd:element[@name='ProgramLevel']/xsd:complexType">
      <jxb:class name="ProgramLevelInfo"/>
    </jxb:bindings>
</jxb:bindings>
0

I solved it by adding XautoNameResolution as an arg in the jaxb plugin configuration.

 <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.14.0</version>
            <executions>
                <execution>
                    <id>schema2</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <args>
                            <arg>-XautoNameResolution</arg>
                        </args>
                        <schemaLanguage>WSDL</schemaLanguage>
                        <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
                        <schemaIncludes>
                            <include>products.wsdl</include>
                        </schemaIncludes>
                        <generatePackage>com.suhas.generated</generatePackage>
                        <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
                        <cleanPackageDirectories>true</cleanPackageDirectories>
                    </configuration>
                </execution>
            </executions>
        </plugin>