XML模式扩展顺序 [英] Xml schema extension order

查看:62
本文介绍了XML模式扩展顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有扩展名,如何确保派生元素在基类元素的前面?缺省情况是相反的.我很想使用all,但是我知道那是不可能的.

If I have an extension, how can I assure that the derived elements are in front of the base class elements? The default is the other way around. I would have loved to use all, but I know that is impossible.

<xs:complexType name="BaseClass">
  <xs:sequence>
    <xs:element name="foo" type="xs:string/>
    <xs:element name="bar" type="xs:string/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="DerivedClass">
  <xs:complexContent>
    <xs:extension base="BaseClass">
      <xs:sequence>
        <!-- This makes the order foo, bar, cheese, tomato -->
        <!-- But I need cheese, tomato, foo, bar -->
        <xs:element name="cheese" type="xs:string/>
        <xs:element name="tomato" type="xs:string/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
<xs:element name="BaseClass" type="BaseClass"/>
<xs:element name="DerivedClass" type="DerivedClass" substitutionGroup="BaseClass"/>

我希望被接受的xml看起来像这样:

The xml I would like to be accepted looks something like this:

<mylist>
    <BaseClass>
     <foo>lala</foo>
     <bar>lala</bar>
    </BaseClass>
    <DerivedClass>
     <cheese>cheddar</cheese>
     <tomato>red</tomato>
     <foo>lala</foo>
     <bar>lala</bar>
    </DerivedClass>
</mylist>

目前,我正在考虑将BaseClass的所有元素也复制到DerivedClass中,但是我不知道替换组会发生什么,什么不发生.

At the moment I'm thinking of just copying all elements of BaseClass into DerivedClass as well, but I don't know what happens with substitutiongroups and what not.

推荐答案

如果我有扩展名,如何确保派生元素在基类元素的前面?

If I have an extension, how can I assure that the derived elements are in front of the base class elements?

不幸的是,这是不可能的. 扩展complexType时,新元素将按顺序添加到基本元素中. 基本上,新的内容模型将如下所示:

Unfortunately, it is impossible. When a complexType is extended, the new elements are added to the base elements as sequence. Basically, the new content model will looks as follows:

(base element model), (extension element model)

这就是XSD中类型扩展的工作方式. 您可能想知道为什么? 因为扩展类型必须符合基本类型. 您不能将任何东西作为任何事物的扩展作为基础.

That's how the type extension in XSD works. You may wonder why? Because the extension type must comply with the base type. You cannot have anything as an extension of anything as a base.

假设您有一个知道如何处理A型元素的软件. 该软件可能会假设实际上可以扩展类型A, 但无论如何,它必须能够在应该是A类型的元素中找到 它从类型A知道/期望的一切...以及类型A固有的元素内容 (这是最重要的事情之一)必须排在第一位!

Suppose you have a software that knows how to process elements of type A. The software may assume that actually the type A might be extended, but in any case, it must be able to find in an element supposed to be of type A everything it knows/expect from type A... and the element content inherent to type A (which is one of the most important things) must come the first!

这篇关于XML模式扩展顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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