With XML Schema, one element can be replaced by another. 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. 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: A valid XML document looks like this (according to the schema above): Or something like this: To prevent other elements from replacing a specified element, use the block attribute: Take a look at a clip of a XML schema: A legitimate XML document should look like this (according to the schema above): But the following documents are no longer legal: 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! 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.Element substitution ¶
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
<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"/>
<customer>
<name>John Smith</name>
</customer>
<kunde>
<navn>John Smith</navn>
</kunde>
Prevent element substitution ¶
<xs:element name="name" type="xs:string" block="substitution"/>
<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"/>
<customer>
<name>John Smith</name>
</customer>
<kunde>
<navn>John Smith</navn>
</kunde>
Use substitutionGroup ¶
What is a global element (Global Elements)? ¶