xsd:如何使用无序列表的元素扩展类型 [英] xsd: How to extend a type with an unordered list of elements
问题描述
这是我的 xml 架构的一部分
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?
推荐答案
发布此问题和已接受的答案一年半后,XSD 1.1 发布.在此版本中,可以指定 OP 要求的内容,因为取消了对 xs:all
的许多限制.其中之一是现在可以扩展 xs:all
.
One-and-half year after this question and the accepted answer were posted, XSD 1.1 was published. In this version it is possible to specify what the OP asked for because a number of restriction on xs:all
were lifted. One of them is that it is now possible to extend an xs:all
.
使用 XSD 1.1,您可以指定以下内容:
Using XSD 1.1 you can specify the following:
<xs:complexType name="Person" abstract="true">
<xs:all>
<xs:element name="name" type="xs:string" minOccurs="0" />
<xs:element name="phone" type="xs:string" minOccurs="0" />
</xs:all>
</xs:complexType>
<xs:complexType name="Friend">
<xs:complexContent>
<xs:extension base="Person">
<xs:all>
<xs:element name="address" type="xs:string" minOccurs="0" />
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Coworker">
<xs:complexContent>
<xs:extension base="Person">
<xs:all>
<xs:element name="office" type="xs:string" minOccurs="0" />
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>
这定义了以下类型:
Person
:具有可选的无序name
和phone
元素的抽象类型;Friend
:扩展Person
,在无序元素列表中添加一个可选的address
元素;Coworker
:扩展Coworker
,将可选的office
元素添加到无序元素列表中.
Person
: an abstract type with optional unorderedname
andphone
elements;Friend
: extendsPerson
adding an optionaladdress
element to the list of unordered elements;Coworker
: extendsCoworker
adding an optionaloffice
element to the list of unordered elements.
请注意,此解决方案并不适用于每个 XML 处理器:尽管自 XSD 1.1 发布以来已经过去了 8 年,但许多处理器仍然只支持 XSD 1.0.
Note that this solution does not work for every XML processor: even though 8 years have passed since the publication of XSD 1.1, a lot of processors still only support XSD 1.0.
这篇关于xsd:如何使用无序列表的元素扩展类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!