Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML Schema element replacement


May 28, 2021 XML Schema


Table of contents


XSD Element Replacement (Element Substitution).

In XML Schema, if you want to specify an element to replace another element, you can add a substitution Group property to the element, and the property value is the name of the element that the element wants to replace.

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


Element replacement

Let's give an example: Our users are from the UK and Norway. We want to have the ability to give users the ability to choose between an element name in Norwegian or an element name in English in an XML document.

To solve this problem, we can define a substitution group in XML schema. First, we declare the main element, and then we declare the secondary elements, which declare that they can replace the main element.

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

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

Take a look at a fragment 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 is something like this (based on schema above):

<customer>
<name>John Smith</name>
</customer>

Or something like this:

<kunde>
<navn>John Smith</navn>
</kunde>

XML Schema Element Replacement Notes:

1. Both the replacement element and the replaced element must be declared using the global element;

2. The replacement element either has the same data type as the replaced element, or the replacement element type is a derived type of the replaced element type.


Block element replacement

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

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

Look at a fragment of an 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"/>

Legitimate XML documents should look something like this (according to schema above):

<customer>
<name>John Smith</name>
</customer>

However, the following documents are no longer legal:

<kunde>
<navn>John Smith</navn>
</kunde>


Use substitution Group

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

Note that all elements in the substitution Group (main and replaceable) must be declared global or they will not work!


What is Global Elements?

A global element is a direct child of a "schema" element! Local elements are elements nested in other elements.