我有一个CommonTypes.xsd,我使用xs:include包含在我的所有其他XSD中.我明白了
Multiple <schemaBindings> are defined for the target namespace ""
Run Code Online (Sandbox Code Playgroud)
当我尝试使用绑定文件将其编译到不同的包中时.请告诉我是否有办法将它们编译成不同的包.我正在使用jaxb 2.1
小智 6
是的,有一种方法.
假设:
xsd/common/common.xsd
xsd/foo/foo.xsd
Run Code Online (Sandbox Code Playgroud)
在公共目录中common.xjb:
<jxb:schemaBindings>
<jxb:package name="mypkg.common">
</jxb:package>
</jxb:schemaBindings>
Run Code Online (Sandbox Code Playgroud)
在foo目录中foo.xjb:
<jxb:schemaBindings>
<jxb:package name="mypkg.foo">
</jxb:package>
</jxb:schemaBindings>
Run Code Online (Sandbox Code Playgroud)
在build.xml文件中,为每个创建一个xjc任务:
<xjc destdir="${app.src}" readOnly="true" package="mypkg.common">
<schema dir="${app.schema}/common" includes="*.xsd" />
<binding dir="${app.schema}/common" includes="*.xjb" />
</xjc>
<xjc destdir="${app.src}" readOnly="true" package="mypkg.foo">
<schema dir="${app.schema}/foo" includes="*.xsd" />
<binding dir="${app.schema}/foo" includes="foo.xjb" />
</xjc>
Run Code Online (Sandbox Code Playgroud)
你需要确保common.xsd有一个targetNameSpace是从不同foo.xsd的targetNameSpace.
如Ben所说,如果它们具有相同的命名空间,则无法做到这一点.但是如果你有不同的命名空间怎么做呢?
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<jxb:bindings namespace="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" schemaLocation="oagi/Common/UNCEFACT/ATG/CoreComponents/UnqualifiedDataTypes.xsd" >
<jxb:schemaBindings>
<jxb:package name="com.test.oagi.udt"/>
</jxb:schemaBindings>
</jxb:bindings>
<jxb:bindings namespace="http://www.openapplications.org/oagis/9/codelists" schemaLocation="oagi/Common/OAGi/Components/CodeLists.xsd" >
<jxb:schemaBindings>
<jxb:package name="com.test.oagi.cl"/>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)
但请确保您不使用命令行参数-p,因为这将覆盖您的配置.