解决 JAXB 编译包含的 XSD 中的命名冲突

发布于 2024-08-30 06:53:44 字数 2596 浏览 8 评论 0原文

我目前正在尝试使用 JAXB(IBM build 2.1.3)将一对模式文件编译到同一个包中。每个都会单独编译,但是当尝试将它们编译在一起时,由于包含而出现元素命名冲突。我的问题是;有没有办法通过外部绑定指定命名冲突的解决方案。

示例文件如下。在示例中,有问题的元素称为“Common”,它在 incA 和 incB 中均定义:

incA.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/"
    xmlns:tns="http://www.example.org/" elementFormDefault="qualified">
    <complexType name="TypeA">
        <sequence>
            <element name="ElementA" type="string"></element>
        </sequence>
    </complexType>
    <!-- Conflicting element -->
    <element name="Common" type="tns:TypeA"></element>
</schema>

incB.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/"
    xmlns:tns="http://www.example.org/" elementFormDefault="qualified">
    <complexType name="TypeB">
        <sequence>
            <element name="ElementB" type="int"></element>
        </sequence>
    </complexType>
    <!-- Conflicting element -->
    <element name="Common" type="tns:TypeB"></element>
</schema>

A.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.example.org/"
    elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.example.org/">
    <include schemaLocation="incA.xsd"></include>
    <complexType name="A">
        <sequence>
            <element ref="tns:Common"></element>
        </sequence>
    </complexType>
</schema>

B.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.example.org/"
    elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.example.org/">
    <include schemaLocation="incB.xsd"></include>
    <complexType name="B">
        <sequence>
            <element ref="tns:Common"></element>
        </sequence>
    </complexType>
</schema>

当两者都是从 xjb 的一次调用编译时,会出现编译器错误:(

[ERROR] 'Common' is already defined
 line 9 of file:/C:/temp/incB.xsd
[ERROR] (related to above error) the first definition appears here
 line 9 of file:/C:/temp/incA.xsd

作为参考,此是解决编译 OAGIS8 SP3 包问题的概括)

I am currently trying to compile with JAXB (IBM build 2.1.3) a pair of schema files into the same package. Each will compile on it's own, but when trying to compile them together i get a element naming conflict due to includes. My question is; is there a way to specify with an external binding a resolution to the naming collision.

Example files follow. In the example the offending element is called "Common", which is defined in both incA and incB:

incA.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/"
    xmlns:tns="http://www.example.org/" elementFormDefault="qualified">
    <complexType name="TypeA">
        <sequence>
            <element name="ElementA" type="string"></element>
        </sequence>
    </complexType>
    <!-- Conflicting element -->
    <element name="Common" type="tns:TypeA"></element>
</schema>

incB.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/"
    xmlns:tns="http://www.example.org/" elementFormDefault="qualified">
    <complexType name="TypeB">
        <sequence>
            <element name="ElementB" type="int"></element>
        </sequence>
    </complexType>
    <!-- Conflicting element -->
    <element name="Common" type="tns:TypeB"></element>
</schema>

A.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.example.org/"
    elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.example.org/">
    <include schemaLocation="incA.xsd"></include>
    <complexType name="A">
        <sequence>
            <element ref="tns:Common"></element>
        </sequence>
    </complexType>
</schema>

B.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.example.org/"
    elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.example.org/">
    <include schemaLocation="incB.xsd"></include>
    <complexType name="B">
        <sequence>
            <element ref="tns:Common"></element>
        </sequence>
    </complexType>
</schema>

Compiler error when both are compiled from one evocation of xjb:

[ERROR] 'Common' is already defined
 line 9 of file:/C:/temp/incB.xsd
[ERROR] (related to above error) the first definition appears here
 line 9 of file:/C:/temp/incA.xsd

(For reference, this is a generalization to resolve an issue with compiling the OAGIS8 SP3 package)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-09-06 06:53:44

通过进一步的研究,我已经确定,由于名称空间冲突,尝试一次编译所有这些片段是不可能的。我决定的解决方法是将每组模式子集编译到它们自己的包中,并在尝试解组之前对传入的 XML 执行启发式测试。

I have determined though further research that trying to compile all these fragments at once is impossible, due to the name-space collision. The work around I decided upon was to compile each set of schema subsets into their own packages, and perform a heuristic test on the incoming XML before attempting to unmarshal it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文