使用 XML Schema 扩展元素而不是 complexType

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1499836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:46:42  来源:igfitidea点击:

Use XML Schema to extend an element rather than a complexType

xmlxsd

提问by Mark Elliot

Suppose I have some schema:

假设我有一些架构:

<xsd:schema ...>
    <xsd:element name="foo">
         <xsd:complexType>
             <xsd:sequence>
                 <xsd:element name="fooElement" type="xs:string" />
             </xsd:sequence>
         </xsd:complexType>
    </xsd:element>
</xsd:schema>

This defines some element foowhich has inside it some element fooElementwith type string. I'd like to now extend element footo also have an element barElementand call this extension bar.

这定义了一些元素foo,其中包含一些fooElement字符串类型的元素。我现在想扩展 elementfoo也有一个 elementbarElement并调用这个 extension bar

To complicate things, let's also suppose that someone else has defined fooand the schema cannot be changed. While the example here is trivial, let's also assume that the contents of foomight be more complicated, and that defining a new schema isn't quite as simple as copying the element fooElement.

更复杂的是,我们还假设其他人已经定义foo并且架构无法更改。虽然这里的示例很简单,但我们还假设 的内容foo可能更复杂,并且定义新模式并不像复制元素那么简单fooElement

Effectively, I'd like to define a new schema:

实际上,我想定义一个新模式:

<xsd:schema xmlns:fns="otherschema" ...>
    <xsd:import namespace="otherschema" />
    <xsd:element name="bar">
         <xsd:complexContent>
             <xsd:extension base="fns:foo">
                 <xsd:sequence>
                     <xsd:element name="barElement" type="xs:string" />
                 </xsd:sequence>
             </xsd:extension>
         </xsd:complexContent>
    </xsd:element>
</xsd:schema>

Unfortunately <xsd:extension>'s baseattribute only accepts XSD type arguments, not elements. How do I extend an element? (can I extend an element?)

不幸的是<xsd:extension>, 的base属性只接受 XSD 类型的参数,而不接受元素。如何扩展元素?(我可以扩展一个元素吗?)

采纳答案by Ben

I would create a new type that mimics foo element and then extend from it. While it's not the best solution, you do get the same result as if you were able to extend the element directly.

我会创建一个模仿 foo 元素的新类型,然后从中扩展。虽然这不是最好的解决方案,但您确实获得了与直接扩展元素相同的结果。

  <xs:complexType name="fooType">
    <xs:sequence>
      <xs:element name="fooElement" type="xs:string" />
    </xs:sequence>
   </xs:complexType>

   <xs:complexType name="barType">
    <xs:complexContent mixed="false">
      <xs:extension base="fooType">
        <xs:sequence>
          <xs:element name="barElement" type="xs:string" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:element name="bar" type="barType" />