Help please: xjc throws "Two declarations cause a collision in the ObjectFactory class"

Take the following simplified XSD:

<?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="com.acme" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Widget"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="color" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="WidgetColor" type="xs:string" /> </xs:schema> 

Then follow these steps:

 xjc test.xsd 

You should definitely get the following exception:

 parsing a schema... compiling a schema... [ERROR] Two declarations cause a collision in the ObjectFactory class. line 11 of file:/C:/test.xsd [ERROR] (Related to above error) This is the other declaration. line 7 of file:/C:/test.xsd Failed to produce code. 

Note that there is a widget element name that is complexType and has elements named color . At the same level as the Widget element, there is a simple element called WidgetColor .

What is more puzzling is that if you remove the minOccurs = "0" OR attribute, you remove the nillable = "true" attribute from the sequence of color elements, xjc will successfully compile the circuit.

Has anyone seen this problem or can offer a solution?

Thanks,

Mike.

+6
source share
1 answer

Well, finally, I figured out how to fix my problem. It consists in using a custom binding to specify a separate class name for one of the declarations.

Content custom-binding.xjb

 <?xml version="1.0" encoding="UTF-8"?> <bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <bindings schemaLocation="test.xsd"> <bindings node="//xs:element[@name='WidgetColor']"> <class name="BaseWidgetColor" /> </bindings> </bindings> </bindings> 

Operation:

 C:\>xjc -b custom-binding.xjb test.xsd parsing a schema... compiling a schema... acme\com\BaseWidgetColor.java acme\com\ObjectFactory.java acme\com\Widget.java acme\com\package-info.java 

Patience et longueur de temps valent mieux que rage et acharnement ...!

+6
source

Source: https://habr.com/ru/post/897679/


All Articles