2

I am attempting to assign different name spaces to different xsd files and use jaxb2-maven plugin to build the artifacts defined by these xsd files.

Maven fails to generate the source with the following error: The namespace of element 'bindings' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'

Here is my configuration:

<jaxb:bindings 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb 
                http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">

<jaxb:bindings schemaLocation="xsd/TheRequest.xsd" node="/xsd:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="com.package.request" />
    </jaxb:schemaBindings>
</jaxb:bindings>

<jaxb:bindings schemaLocation="xsd/TheResponse.xsd" node="/xsd:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="com.package.response" />
    </jaxb:schemaBindings>
</jaxb:bindings>

</jaxb:bindings>

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://company.services.com" 
xmlns:tns="http://company.services.com" 
elementFormDefault="unqualified">

<xsd:complexType name="FindSomething">
    <xsd:sequence>
        <xsd:element name="TestMode" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="Channel" type="xsd:string" maxOccurs="1" minOccurs="1"/>
        <xsd:element name="UserId" type="xsd:string" maxOccurs="1" minOccurs="1"/>
        <xsd:element name="Role" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="Format" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="OrgId" type="xsd:string" maxOccurs="1" minOccurs="1"/>
        <xsd:element name="TransactionId" type="xsd:string" maxOccurs="1" minOccurs="1"/>
        <xsd:element name="Timeout" type="xsd:long" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="RequestSegments" type="tns:RequestSegments" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="VerifyUserType" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="VerifyUserAccess" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="IncludeFamily" type="xsd:string" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="AsOfDate" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="ActiveOnly" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="SearchType" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="SearchCriteria" type="tns:SearchCriteria" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="AccessPrivileges" type="tns:AccessPrivileges" maxOccurs="1" minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType></xsd:schema>

I have tried with different forms of XML namespace directives. In Eclipse code completion, I can see the bindings XML schema as an option so I don't know why maven is returning this error.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Half_Duplex
  • 5,102
  • 5
  • 42
  • 58

2 Answers2

5

The maven jaxb2 plugin is finding your binding file in its source path and assuming that it is an xsd file. You'll have to exclude it, either by moving it, updating your xsd/xjb source paths, or by adding an exclude filter.

The easiest option is probably to update the paths in the plugin config:

<configuration>
    <sources>
        <!-- only xsd files under here -->
        <source>src/main/xjb/xsd</source>
    </sources>
    <xjbSources>
        <!-- specify binding file explicitly -->
        <xjbSource>src/main/xjb/bindings.xjb</xjbSource>
    </xjbSources>
</configuration>

There's also a problem with your binding file: you'll need to map the namespace prefix you used in the node attribute. Add xmlns:xsd="http://www.w3.org/2001/XMLSchema" to the root element.

The config options are detailed in the doc for the jaxb2:xjc goal.

teppic
  • 7,051
  • 1
  • 29
  • 35
  • Thanks, I've added the xjbSources which corrects the original issue. I am now back to a previous problem where a namespace duplicated between the 2 xsd's causes a failure. I thought the XJB with predefined package names was the solution for this. Thoughts? – Half_Duplex Jan 10 '17 at 15:05
  • 2
    Classes for elements in the same namespace can't be generated in different packages, so you'll have to resolve any naming collisions you have by renaming the classes themselves (which can be done in the binding file). – teppic Jan 10 '17 at 19:05
  • OK Thank you. I was under the impression duplicates could be handled by being created in different packages as specified in the binding file. – Half_Duplex Jan 10 '17 at 19:25
  • Could you please point me to an example of this? I am attempting to rename the class with this code. ` ` with the same result as above. – Half_Duplex Jan 10 '17 at 19:59
1

I ran into the same error message and tried the solution suggested by @teppic, which unfortunately did not help.

I found out that having the binding file and the XSD file sharing the same name is causing the same error message! I had something like this:

example.xjb and examle.xsd

After renaming the .xjb file to example-binding.xjb everything worked for me!

Hope this helps in 2020 ^^

gerstams
  • 415
  • 2
  • 12