JAXB :: xjc 2.2无法识别简单类型的绑定

时间:2013-05-29 13:52:42

标签: jaxb xjc

我有一堆 xsd 架构,其中一些导入其他架构。因此,每当我在导入其他文件的某些 xsd 文件上调用xjc(Ant任务或命令行工具)时,我都会传递一个绑定文件以确保导入的类不是再次生成(因为它们已经为“upstread”模式生成)。此解决方案在this SO post (answer by Blaise Doughan)中进行了描述。

虽然这种方法适用于 xsd:complexType 类型,但它似乎不适用于 xsd:simpleType 类型。这是一个缩小的例子来证明这一点。假设我们有两个死简单模式,一个导入另一个:

a.xsd   ---import-->  b.xsd

文件是:

a.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://example.a" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:import namespace="http://example.b"
              schemaLocation="b.xsd"/>

</xs:schema>

b.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://example.b" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:b="http://example.b" 
           elementFormDefault="qualified">

   <xs:simpleType name="ResourceType">
      <xs:restriction base="xs:string">
         <xs:enumeration value="alpha"/>
         <xs:enumeration value="beta"/>
      </xs:restriction>
   </xs:simpleType>

   <xs:complexType name="ResourceType2">
        <xs:sequence>
            <xs:element name="a" type="xs:integer"/>
        </xs:sequence>
   </xs:complexType>
</xs:schema>

所以基本上 a.xsd 什么也不做,只导入 b.xsd 。当一个人在 a.xsd 上调用 xjc 时,如下所示:

xjc -d output -p example.a -npa a.xsd 

你得到:

output/
└── example
    └── a
        ├── ObjectFactory.java
        ├── ResourceType2.java
        └── ResourceType.java

因此为导入的模式生成了文件。由于我们不希望这样,我们定义以下绑定文件来传递:

进口-bindings.xml

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="b.xsd">
        <jxb:bindings node="//xs:simpleType[@name='ResourceType']">
            <jxb:class ref="example.b.ResourceType"/>
        </jxb:bindings>
        <jxb:bindings node="//xs:complexType[@name='ResourceType2']">
            <jxb:class ref="example.b.ResourceType2"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

HOWEVER 当我们使用 -b 选项再次在 a.xsd 上调用 xjc 时,如下所示:< / p>

xjc -d output -p a.example a.xsd -b imported-bindings.xml 

我们在输出目录中看到以下内容:

output/
└── example
    └── a
        ├── ObjectFactory.java
        └── ResourceType.java

也就是说,确实抑制了 xsd:complexType “ResourceType2”的生成,但 xjc 继续生成并为导入的 xsd:simpleType <生成代码/ strong>“ResourceType”。有什么想法吗?

更新 除了使用自动生成的剧集文件

的解决方法之外,我仍然没有这个工作

1 个答案:

答案 0 :(得分:-1)

JAXB 2.2.x版本仅支持我猜。