【问题标题】:Error in jaxb2-maven-plugin XJB: The namespace of element 'bindings' must be from the schema namespace'jaxb2-maven-plugin XJB 中的错误:元素“绑定”的命名空间必须来自模式命名空间“
【发布时间】:2017-01-09 21:30:51
【问题描述】:

我正在尝试为不同的 xsd 文件分配不同的名称空间,并使用 jaxb2-maven 插件来构建由这些 xsd 文件定义的工件。

Maven 生成源失败,报错:The namespace of element 'bindings' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'

这是我的配置:

<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>

我尝试过使用不同形式的 XML 命名空间指令。在 Eclipse 代码完成中,我可以看到绑定 XML 模式作为一个选项,所以我不知道为什么 maven 会返回这个错误。

【问题讨论】:

  • 你的插件配置是什么?

标签: maven xsd jaxb jaxb2-maven-plugin xjb


【解决方案1】:

maven jaxb2 插件在其源路径中查找您的绑定文件,并假设它是一个 xsd 文件。您必须通过移动它、更新 xsd/xjb 源路径或添加排除过滤器来排除它。

最简单的选择可能是更新插件配置中的路径:

<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>

您的绑定文件也存在问题:您需要映射您在node 属性中使用的命名空间前缀。将xmlns:xsd="http://www.w3.org/2001/XMLSchema" 添加到根元素。

jaxb2:xjc 目标的文档中详细介绍了配置选项。

【讨论】:

  • 谢谢,我添加了 xjbSources 来纠正原始问题。我现在回到之前的问题,即在 2 个 xsd 之间重复的命名空间导致失败。我认为具有预定义包名称的 XJB 是解决此问题的方法。想法?
  • 同一命名空间中元素的类不能在不同的包中生成,因此您必须通过重命名类本身来解决任何命名冲突(可以在绑定文件中完成) )。
  • 好的,谢谢。我的印象是可以通过在绑定文件中指定的不同包中创建重复项来处理。
  • 你能给我举个例子吗?我正在尝试使用此代码重命名该类。 &lt;jxb:bindings node="//xsd:element[@name='Speciality']"&gt; &lt;jxb:class name="SpecialityResponse" /&gt; &lt;/jxb:bindings&gt; 与上述结果相同。
【解决方案2】:

我遇到了同样的错误消息并尝试了@teppic 建议的解决方案,但不幸的是没有帮助。

我发现绑定文件和 XSD 文件共享同名会导致相同的错误消息!我有这样的事情:

example.xjb 和 例子.xsd

将 .xjb 文件重命名为 example-binding.xjb 后,一切都对我有用!

希望这对 2020 年有所帮助^^

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 2014-02-15
    • 2015-06-21
    • 2012-04-13
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    相关资源
    最近更新 更多