使用 XML 模式作为 OO 继承 (JAXB) - 可能吗?

发布于 2024-10-02 07:27:31 字数 1865 浏览 3 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(2

分分钟 2024-10-09 07:27:31

如果IDResponse正在扩展Response,则“complexType”应该是“complexContent”,并且“restriction”应该是“extension”。

If IDResponse is extending Response, "complexType" should be "complexContent", and "restriction" should be "extension".

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