如何使用扩展名在 xsd:sequence 之前插入?
How to use extension to insert before an xsd:sequence?
在所有关于扩展序列的例子中,所有的新元素都附加在最后。
请参阅 personinfo
和 fullpersoninfo
,网址为:
http://www.w3schools.com/schema/schema_complex.asp
如何定义一个新的序列通过扩展在前面插入新的元素?示例(第二部分错误;如何更正?):
<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
位于许多序列的末尾。
示例:
<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
不是那样工作的。详细信息如下,还有一个替代建议...
XSD 1.0
扩展不能在序列之前插入新元素;它们必须附加在 序列之后。根据 XSD 1.0 规范,XML Schema Part 1: Structures Second Edition:
A complex type which extends another does so by having additional
content model particles at the end of the other definition's content
model, or by having additional attribute declarations, or both.
- 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 Schema Definition Language (XSD) 1.1 Part 1: Structures:
A complex type which extends another does so by having additional
content model particles at the end of the other definition's content
model, or by having additional attribute declarations, or both.
- 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
来提取公共元素定义并在它们之前插入新元素。
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>
在所有关于扩展序列的例子中,所有的新元素都附加在最后。
请参阅 personinfo
和 fullpersoninfo
,网址为:
http://www.w3schools.com/schema/schema_complex.asp
如何定义一个新的序列通过扩展在前面插入新的元素?示例(第二部分错误;如何更正?):
<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
位于许多序列的末尾。
示例:
<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
不是那样工作的。详细信息如下,还有一个替代建议...
XSD 1.0
扩展不能在序列之前插入新元素;它们必须附加在 序列之后。根据 XSD 1.0 规范,XML Schema Part 1: Structures Second Edition:
A complex type which extends another does so by having additional content model particles at the end of the other definition's content model, or by having additional attribute declarations, or both.
- 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 Schema Definition Language (XSD) 1.1 Part 1: Structures:
A complex type which extends another does so by having additional content model particles at the end of the other definition's content model, or by having additional attribute declarations, or both.
- 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
来提取公共元素定义并在它们之前插入新元素。
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>