xsd: How to extend a type with an unordered list of elements

≯℡__Kan透↙ 提交于 2020-01-11 08:17:06

问题


This is a part of my xml schema

<xs:complexType name="Friend">
    <xs:all>
        <xs:element name="name" type="xs:string" />
        <xs:element name="phone" type="xs:string" />
        <xs:element name="address" type="xs:string" />
    </xs:all>
</xs:complexType>

<xs:complexType name="Coworker">
    <xs:all>
        <xs:element name="name" type="xs:string" />
        <xs:element name="phone" type="xs:string" />
        <xs:element name="office" type="xs:string" />
    </xs:all>
</xs:complexType>

For better maintainability, I would like to have the shared attributes in an (abstract) super type or something like that. But more important, I want that all elements are unordered and also optional.

Is this possible, and what is the best way to do it?


回答1:


You have to limit yourself a little bit, some of the things you are trying to do are not possible in XML Schema.

Suppose you introduce a complex type called Person to be a super-type of Friend and Coworker. Here are your options:

  1. Replace xs:all with xs:sequence, remove name and phone from the sub-types, add to the super-type, and add inheritance. Your elements now have to be ordered, but you can make them individually optional. It is illegal to use xs:all in type hierarchies in XML Schema, because the processor cannot tell where the parent content model stops and the child content model starts.
  2. Replace xs:all with <xs:choice maxOccurs="unbounded"> in both types, and add your inheritance. Then your elements become unordered again, but they may repeat.

So in conclusion: given your type names up there, I would guess that your requirements will not be exactly met. I would go for the first option: insisting on arbitrary element order is often not as useful as it seems.



来源:https://stackoverflow.com/questions/3808156/xsd-how-to-extend-a-type-with-an-unordered-list-of-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!