问题描述
在有关扩展序列的所有示例中,所有新元素都附加在末尾.请参阅 personinfo
和 fullpersoninfo
在:http://www.w3schools.com/schema/schema_complex.asp
In all examples about extending a sequence, all new elements are appened at the end.See personinfo
and fullpersoninfo
at :http://www.w3schools.com/schema/schema_complex.asp
如何通过扩展定义一个新序列以在 之前插入新元素?示例(第二部分错误;如何更正?):
How to define a new sequence by extension to insert new elements before ? Example (the 2nd part is wrong; how to correct it ?) :
<xs:complexType name="address">
<xs:sequence>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:extension base="address"/>
</xs:complexContent>
</xs:complexType>
目的是验证city
和country
位于许多序列末尾的一些元素.
The aim is to validate some elements where city
and country
are at the end of many sequences.
示例:
<Employee>
<name>A.Miller</name>
<city>Madrid</city>
<country>Spain</country>
</Employee>
<Flight>
<airport>CDG</airport>
<city>Paris</city>
<country>France</country>
</Flight>
推荐答案
正如@Xstian 在评论中提到的,xs:extension
不能那样工作.详细信息如下,以及替代建议...
As @Xstian mentioned in the comments, xs:extension
doesn't work that way. Details follow, along with an alternative suggestion...
扩展不能在序列之前插入新元素;它们必须附加在 序列之后.根据 XSD 1.0 规范,XML 架构第 1 部分:结构第二版:
扩展另一个复杂类型其他定义内容末尾的内容模型粒子模型,或通过附加属性声明,或两者兼而有之.
- 注意:此规范仅允许附加,不允许其他类型的扩展.这一决定简化了申请将实例从派生类型转换为基类型所需的处理.未来的版本可能允许更多种类的扩展,需要更多实现投射的复杂转换.
- Note: This specification allows only appending, and not other kinds of extensions. This decision simplifies application processing required to cast instances from derived to base type. Future versions may allow more kinds of extension, requiring more complex transformations to effect casting.
XSD 1.1
支持一些特殊情况,但仍然不允许在您查找时在序列之前插入的情况.根据 XSD 1.1 规范,W3C XML 模式定义语言 (XSD) 1.1 第 1 部分:结构:
扩展另一个复杂类型其他定义内容末尾的内容模型粒子模型,或通过附加属性声明,或两者兼而有之.
- 注意:在大多数情况下,此规范仅允许附加,而不允许其他类型的扩展.这个决定简化从投射实例所需的应用程序处理派生类型到基类型.一种特殊情况允许以不保证新的方式扩展所有组材料仅出现在内容的末尾.另一个特别案例是在交错模式下通过开放内容扩展.
- Note: For the most part, this specification allows only appending, and not other kinds of extensions. This decision simplifies application processing required to cast instances from the derived type to the base type. One special case allows the extension of all-groups in ways that do not guarantee that the new material occurs only at the end of the content. Another special case is extension via Open Contents in interleave mode.
替代建议:群组
您可以使用 xs:group
来分解公共元素定义并在它们之前插入新元素,而不是 xs:extension
.
Alternative Suggestion: Groups
Instead of xs:extension
you could use xs:group
to factor out the common element definitions and insert new elements before them.
XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Employee>
<name>A.Miller</name>
<city>Madrid</city>
<country>Spain</country>
</Employee>
<Flight>
<airport>CDG</airport>
<city>Paris</city>
<country>France</country>
</Flight>
</root>
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="AddressGroup">
<xs:sequence>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:complexType name="EmployeeType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:group ref="AddressGroup"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="FlightType">
<xs:sequence>
<xs:element name="airport" type="xs:string"/>
<xs:group ref="AddressGroup"/>
</xs:sequence>
</xs:complexType>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" type="EmployeeType"/>
<xs:element name="Flight" type="FlightType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这篇关于如何使用扩展名在 xsd:sequence 之前插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!