XML和XSD - 使用元素名称替换xsi:type for polymorphism

时间:2021-10-29 17:20:04

Taking the W3C vehicle XSD as an example:

以W3C车辆XSD为例:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://cars.example.com/schema"
           xmlns:target="http://cars.example.com/schema">

  <complexType name="Vehicle" abstract="true"/>

  <complexType name="Car">
    <complexContent>
      <extension base="target:Vehicle"/>
      ...
    </complexContent>
  </complexType>

  <complexType name="Plane">
    <complexContent>
      <extension base="target:Vehicle"/>
      <sequence>
        <element name="wingspan" type="integer"/>
      </sequence>
    </complexContent>
  </complexType>      
</schema>

, and the following definition of 'meansOfTravel':

,以及'meansOfTravel'的以下定义:

<complexType name="MeansOfTravel">
  <complexContent>
    <sequence>        
      <element name="transport" type="target:Vehicle"/>        
    </sequence>
  </complexContent>
</complexType>

<element name="meansOfTravel" type="target:MeansOfTravel"/>

With this definition you need to specify the type of your instance using xsi:type, like this:

使用此定义,您需要使用xsi:type指定实例的类型,如下所示:

<meansOfTravel>
  <transport xsi:type="Plane">
     <wingspan>3</wingspan>
  </transport>
</meansOfTravel>

I would just like to acheive a 'name of type' - 'name of element' mapping so that this could be replaced with just

我只想实现一个'类型名称' - '元素名称'映射,以便可以用它替换它

<meansOfTravel>
  <plane>
    <wingspan>3</wingspan>
  </plane>
</meansOfTravel>

The only way I could do this until now is by making it explicit:

到目前为止我唯一能做到这一点的方法是明确:

<complexType name="MeansOfTravel">
  <sequence>        
    <choice>
      <element name="plane" type="target:Plane"/>
      <element name="car" type="target:Car"/>         
    </choice>
  </sequence>
</complexType>

<element name="meansOfTravel" type="target:MeansOfTravel"/>

But this means that I have to list all possible sub-types in the 'MeansOfTravel' complex type. Is there no way of making the XML parser assume that you mean a 'Plane' if you call the element 'plane'? Or do I have to make the choice explicit? I would just like to keep my design DRY - if you have any other suggestions (like groups or so) - i am all ears.

但这意味着我必须在'MeansOfTravel'复杂类型中列出所有可能的子类型。如果你调用元素'plane',有没有办法让XML解析器假设你是指'Plane'?或者我是否必须明确选择?我只想保持我的设计干 - 如果你有任何其他建议(如团体等) - 我都是耳朵。

1 个解决方案

#1


3  

There is a common design pattern around this, you can use sub-types (as you are already doing), and elements in a substitution group. Elements in the substitution group have to be of a sub-type of the element they are substituted for.

围绕此有一个共同的设计模式,您可以使用子类型(正如您已经在做的那样)和替换组中的元素。替换组中的元素必须是它们被替换的元素的子类型。

Unfortuntaly, substitution group elements need to be defined as global elements. So you would have this:

不幸的是,需要将替换组元素定义为全局元素。所以你会有这个:

<complexType name="MeansOfTravel">
  <complexContent>
    <sequence>        
      <element ref="transport"/>
    </sequence>
  </complexContent>
</complexType>

<element name="transport" type="target:Vehicle"/>
<element name="plane" type="target:Plane" substitutionGroup="target:transport"/>

Then, in your XML document you can use:

然后,在XML文档中,您可以使用:

<meansOfTravel>
    <plane>...</plane>
</meansOfTravel>

More info on substitution groups here. And no, unfortunately the parser cannot guess this, so you still have to list the elements :( There is one advantage over a choice though: the schema can be extended externally, by importing it, without modifying it. The choice could not be extended.

关于替换组的更多信息在这里。不,不幸的是,解析器无法猜测到这一点,所以你仍然需要列出元素:(虽然选择有一个优势:架构可以通过导入来扩展到外部,而不需要修改它。选择无法扩展。

#1


3  

There is a common design pattern around this, you can use sub-types (as you are already doing), and elements in a substitution group. Elements in the substitution group have to be of a sub-type of the element they are substituted for.

围绕此有一个共同的设计模式,您可以使用子类型(正如您已经在做的那样)和替换组中的元素。替换组中的元素必须是它们被替换的元素的子类型。

Unfortuntaly, substitution group elements need to be defined as global elements. So you would have this:

不幸的是,需要将替换组元素定义为全局元素。所以你会有这个:

<complexType name="MeansOfTravel">
  <complexContent>
    <sequence>        
      <element ref="transport"/>
    </sequence>
  </complexContent>
</complexType>

<element name="transport" type="target:Vehicle"/>
<element name="plane" type="target:Plane" substitutionGroup="target:transport"/>

Then, in your XML document you can use:

然后,在XML文档中,您可以使用:

<meansOfTravel>
    <plane>...</plane>
</meansOfTravel>

More info on substitution groups here. And no, unfortunately the parser cannot guess this, so you still have to list the elements :( There is one advantage over a choice though: the schema can be extended externally, by importing it, without modifying it. The choice could not be extended.

关于替换组的更多信息在这里。不,不幸的是,解析器无法猜测到这一点,所以你仍然需要列出元素:(虽然选择有一个优势:架构可以通过导入来扩展到外部,而不需要修改它。选择无法扩展。