使用 XML 模式作为 OO 继承 (JAXB) - 可能吗?
我的 XML 架构文件中有一组复杂类型。基本上我有一堆“响应”对象。每个响应对象都遵循相同的通用格式,但采用更具体的方式。因此,例如:
<xsd:simpleType name="Transition">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:complexType name="ResponseBlock"/>
<xsd:complexType name="Response">
<xsd:complexContent>
<xsd:sequence>
<xsd:element name="transition" type="dto:Transition"/>
<xsd:element name="data" type="dto:ResponseBlock"/>
</xsd:sequence>
</xsd:complexContent>
</xsd:complexType>
我希望有一个“子类”,其中“转换”和“ResponseBlock”被替换为每种类型的有效“子类”。即,我已经通过限制/扩展派生了它们,并希望将它们包含在本身通过限制派生的响应中:
<xsd:simpleType name="IDTransition">
<xsd:restriction base="dto:Transition">
<xsd:enumeration value="NEXT_PAGE"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="IDResponseBlock">
<xsd:complexContent>
<xsd:extension base="dto:ResponseBlock">
<xsd:sequence>
<xsd:element type="xsd:int" name="userID"/>
<xsd:element type="xsd:string" name="userName"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="IDResponse">
<xsd:complexContent>
<xsd:restriction base="Response">
<xsd:sequence>
<xsd:element name="transition" type="dto:IDTransition"/>
<xsd:element name="data" type="dto:IDResponseBlock"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
这样,使用 JAXB,我可以生成完全面向对象的继承结构。但它似乎无法编译; Eclipse 显示错误:
derivation-ok-restriction.5.4.2:类型“IDResponse”错误。类型的粒子不是基粒子的有效限制。 rcase-Recurse.2:粒子之间不存在完整的功能映射。
关于如何实现这一目标有什么想法吗?
I have a set of complex types in my XML schema file. Basically I have a bunch of "response" objects. Each response object follows the same general format but in more specific ways. So, for example:
<xsd:simpleType name="Transition">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:complexType name="ResponseBlock"/>
<xsd:complexType name="Response">
<xsd:complexContent>
<xsd:sequence>
<xsd:element name="transition" type="dto:Transition"/>
<xsd:element name="data" type="dto:ResponseBlock"/>
</xsd:sequence>
</xsd:complexContent>
</xsd:complexType>
I'd like to make it possible to have a "subclass" of this where the "transition" and "ResponseBlock" are replaced with valid "subclasses" of each of those types. I.e. I have already derived them by restriction/extension and want to include them in a Response which is itself derived by restriction:
<xsd:simpleType name="IDTransition">
<xsd:restriction base="dto:Transition">
<xsd:enumeration value="NEXT_PAGE"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="IDResponseBlock">
<xsd:complexContent>
<xsd:extension base="dto:ResponseBlock">
<xsd:sequence>
<xsd:element type="xsd:int" name="userID"/>
<xsd:element type="xsd:string" name="userName"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="IDResponse">
<xsd:complexContent>
<xsd:restriction base="Response">
<xsd:sequence>
<xsd:element name="transition" type="dto:IDTransition"/>
<xsd:element name="data" type="dto:IDResponseBlock"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
This way, using JAXB, I could generate a fully object-oriented inheritance structure. But it doesn't seem to compile; Eclipse is showing an error:
derivation-ok-restriction.5.4.2: Error for type 'IDResponse'. The particle of the type is not a valid restriction of the particle of the base.
rcase-Recurse.2: There is not a complete functional mapping between the particles.
Any ideas on how I can accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下文章与 JAXB 和继承相关:
The following articles are related to JAXB and Inheritance:
如果IDResponse正在扩展Response,则“complexType”应该是“complexContent”,并且“restriction”应该是“extension”。
If IDResponse is extending Response, "complexType" should be "complexContent", and "restriction" should be "extension".