使用 XML Schema 来扩展元素而不是 complexType [英] Use XML Schema to extend an element rather than a complexType

查看:9
本文介绍了使用 XML Schema 来扩展元素而不是 complexType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些架构:

<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>

这定义了一些元素 foo,它里面有一些元素 fooElement 类型为字符串.我现在想扩展元素 foo 也有一个元素 barElement 并调用这个扩展 bar.

This defines some element foo which has inside it some element fooElement with type string. I'd like to now extend element foo to also have an element barElement and call this extension bar.

为了使事情复杂化,我们还假设其他人已经定义了 foo 并且架构无法更改.虽然这里的例子很简单,但我们也假设 foo 的内容可能更复杂,并且定义新模式并不像复制元素 fooElement.

To complicate things, let's also suppose that someone else has defined foo and the schema cannot be changed. While the example here is trivial, let's also assume that the contents of foo might be more complicated, and that defining a new schema isn't quite as simple as copying the element 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>

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

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

推荐答案

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

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.

  <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" />

这篇关于使用 XML Schema 来扩展元素而不是 complexType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆