(我已经用Google搜索并在此处搜索过,但未找到答案,也许我使用了错误的关键字......)
为简单起见,我有两个模式:
a.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://foo.bar/something"
targetNamespace="http://foo.bar/something"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:complexType name="TFoo">
<xs:attribute name="version" type="xs:string" />
</xs:complexType>
</xs:schema>
b.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://foo.bar/something"
targetNamespace="http://foo.bar/something"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:complexType name="TFoo">
<xs:attribute name="version" type="xs:string" />
<xs:attribute name="dateTime" type="xs:dateTime" />
</xs:complexType>
</xs:schema>
两者都有相同的targetNamespace 和名为 TFoo 的complexType。
我有一个外部绑定将 a.xsd 生成的类名从 TFoo 更改为 TFooA :
一个-binding.xml :
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
version="2.1">
<jxb:bindings schemaLocation="a.xsd">
<jxb:bindings node="//xs:complexType[@name='TFoo']">
<jxb:class name="TFooA"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
如果我单独编译 a.xsd ,则有效:
$ xjc -b a-binding.xml a.xsd
parsing a schema...
compiling a schema...
bar/foo/something/ObjectFactory.java
bar/foo/something/TFooA.java
bar/foo/something/package-info.java
(看看我是如何 TFoo A .java )
但是,如果我尝试一次编译两个模式,我得到:
$ xjc -b a-binding.xml a.xsd b.xsd
parsing a schema...
[ERROR] 'TFoo' is already defined
line 13 of file:/home/scherrer/tmp/PL_008f/b.xsd
[ERROR] (related to above error) the first definition appears here
line 9 of file:/home/scherrer/tmp/PL_008f/a.xsd
Failed to parse a schema.
我知道 TFoo 定义了两次,这就是为什么我有外部绑定来解决冲突的原因。
Obs。这两种模式都是虚构的,用于举例说明问题,真实的模式(很多)是由第三方提供的,我无法改变它们。
有人能告诉我这是否是某种xjc限制(它未列出here)或者根本不应该工作?或者也许是一个错误?
提前致谢。
答案 0 :(得分:2)
有2个不同的模式文档定义相同的名称空间(更糟糕的是 - 相同的元素)相当于有2个不同的jar包含相同的包和相同的类。这不是jaxb本身的限制 - 它违反了模式名称空间的含义。
简而言之,您无法一起处理这些模式。
生成器无法创建类,因为它不知道要引用的内容。在您尝试重命名之前发生故障。它在读取模式时发生。
您可以做的是单独处理模式并更改使用的java包名称。这避免了java包空间中的命名冲突,并且基本上将模式文档视为完全独立的实体,彼此之间没有引用。这可以通过定义要在绑定中使用的包名称来完成:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
version="2.1">
<jxb:bindings schemaLocation="a.xsd" node="/xs:schema">
<jxb:schemaBindings>
<jxb:package name="com.foo.a"></jxb:package>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
答案 1 :(得分:-1)
我认为您发现了数据绑定的限制。请尝试data projection(披露:我与该项目有关联)。 您的架构b.xsd看起来就像是a.xsd的扩展。您可以创建适合b.xsd的投影界面,并将其与任何符合a.xsd或b.xsd的文档一起使用。