15.2.17. XSD element substitution (Element Substitution)

发布时间 : 2025-10-25 13:35:23 UTC      

Page Views: 10 views

With XML Schema, one element can be replaced by another.

Element substitution

Let’s give an example: our users are from the UK and Norway. We want to be able to give users the ability to choose whether to use Norwegian or English element names in XML documents.

To solve this problem, we can define a substitutionGroup . First, we declare the primary element, and then we declare the secondary elements, which can declare that they can replace the primary element.

<xs:element name="name" type="xs:string"/> <xs:element name="navn" substitutionGroup="name"/> 

In the above example, the “name” element is the main element, while the “navn” element replaces the “name” element.

Take a look at a clip of XML schema:

<xs:element name="name" type="xs:string"/> <xs:element name="navn" substitutionGroup="name"/> <xs:complexType name="custinfo"> <xs:sequence> <xs:element ref="name"/> xs:sequence> xs:complexType> <xs:element name="customer" type="custinfo"/> <xs:element name="kunde" substitutionGroup="customer"/>    

A valid XML document looks like this (according to the schema above):

<customer> <name>John Smithname> customer>    

Or something like this:

<kunde> <navn>John Smithnavn> kunde>     

Prevent element substitution

To prevent other elements from replacing a specified element, use the block attribute:

<xs:element name="name" type="xs:string" block="substitution"/> 

Take a look at a clip of a XML schema:

<xs:element name="name" type="xs:string" block="substitution"/> <xs:element name="navn" substitutionGroup="name"/> <xs:complexType name="custinfo"> <xs:sequence> <xs:element ref="name"/> xs:sequence> xs:complexType> <xs:element name="customer" type="custinfo" block="substitution"/> <xs:element name="kunde" substitutionGroup="customer"/>    

A legitimate XML document should look like this (according to the schema above):

<customer> <name>John Smithname> customer>    

But the following documents are no longer legal:

<kunde> <navn>John Smithnavn> kunde>     

Use substitutionGroup

The type of the replaceable element must be the same as or derived from the parent element. If the type of the replaceable element is the same as the type of the main element, you do not have to specify the type of the replaceable element.

Note that all elements in the substitutionGroup (primary and replaceable elements) must be declared as global elements or they will not work!

What is a global element (Global Elements)?

The global element refers to the immediate child element of the “schema” element! Local elements (Local elements) refer to elements that are nested within other elements.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。