【问题标题】:JAXB property bindings - why is this even neededJAXB 属性绑定 - 为什么甚至需要它
【发布时间】:2017-08-20 20:20:18
【问题描述】:

我有以下 xsd(我只在此处发布相关部分,但命令也针对此摘录运行)

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
    <xs:complexType name="OptionType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="name" use="optional"/>
                <xs:attribute type="xs:string" name="value" use="optional"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="ControllableType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="xs:float" name="value" use="optional"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>

我对它运行以下命令:

xjc src/main/resources/session.xsd -p org.myorg.mypackage -d src/main/java/org/myorg/mypackage

我收到以下错误:

[ERROR] Property "Value" is already defined. Use &lt;jaxb:property> to resolve this conflict.
line 39 of jar:file:/usr/lib/jvm/java-8-openjdk-amd64/lib/tools.jar!/com/sun/xml/internal/xsom/impl/parser/datatypes.xsd

[ERROR] The following location is relevant to the above error
line 14 of file:/home/user/project/src/main/resources/session.xsd

[ERROR] Property "Value" is already defined. Use &lt;jaxb:property> to resolve this conflict.
line 39 of jar:file:/usr/lib/jvm/java-8-openjdk-amd64/lib/tools.jar!/com/sun/xml/internal/xsom/impl/parser/datatypes.xsd

[ERROR] The following location is relevant to the above error
line 7 of file:/home/user/project/src/main/resources/session.xsd

Failed to parse a schema.

我在这里读到了一些关于绑定的内容:

Symbol is already defined. Use JAXB property to resolve the conflict

JAXB Compiling Issue - [ERROR] Property "Any" is already defined

但是为什么还需要这个呢? XML 元素名称与属性名称结合,属性不是完全限定和唯一的吗?

如“OptionType.value”或“ControllableType.value”

【问题讨论】:

标签: java xml xsd jaxb xjc


【解决方案1】:

问题是你有一个内容简单的复杂类型和一个属性value:

<xs:complexType name="ControllableType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute type="xs:float" name="value" use="optional"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

这将允许类似

<somelement value="1.5">one and a half</somelement>

XJC 尝试创建两个属性:

  • 属性value,
  • 一个简单的内容。

第一个属性默认命名为value,因为这是属性的名称。

第二个属性默认命名为value,因为这是简单内容属性的默认名称。

这会让你发生碰撞。

【讨论】:

    【解决方案2】:

    您似乎正在尝试扩展 java.lang.String。但是由于 String 类已经包含一个“值”属性,所以你不能在你的子类中使用它。-

    我想人们可能会忽略 String 类是最终的,因此不能扩展。当您重命名“值”属性时,您会看到 xjc 可以工作,但它仍然不会在生成的代码中扩展 String(因为它不可能),但 xjc 显然仍然检查您的属性名称是否有效,这会导致问题.

    @lexicore 在 cmets 中解释了为什么这个假设是错误的:


    编辑考虑 lexicores cmets

    所以你的选择是重命名你的“价值”。检查@lexicores 答案以了解原因。

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
    <xs:complexType name="OptionType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="name" use="optional"/>
                <xs:attribute type="xs:string" name="my_value" use="optional"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="ControllableType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="xs:float" name="my_value" use="optional"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    

    或者不在 xs:simpleContent 中使用 xs:extension。

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
    <xs:complexType name="OptionType">
        <xs:attribute type="xs:string" name="name" use="optional"/>
        <xs:attribute type="xs:string" name="value" use="optional"/>
    </xs:complexType>
    <xs:complexType name="ControllableType">
        <xs:attribute type="xs:float" name="value" use="optional"/>
    </xs:complexType>
    

    【讨论】:

    • 不,这与扩展 java.lang.String 无关(无论如何这是不可能的)。
    • 那么 做什么呢?
    • 为复杂类型的简单内容定义基类型。您写了 看起来您正在尝试扩展 java.lang.String。 这不是这里发生的事情。
    • 酷。起初我没有明白这一点。那么我会编辑我的答案还是删除它,因为它是错误的?
    • 我只是用粗体添加一个注释,答案显然不正确。删除与否由你决定。如果您愿意并觉得我的回答不够充分,请对其进行编辑。
    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 2016-09-24
    相关资源
    最近更新 更多