Easy Tutorial
❮ Schema Intro El Complextype ❯

Element Substitution in XSD


Through XML Schema, one element can be substituted for another.


Element Substitution

Let's illustrate this with an example: Our users are from the UK and Norway. We want to have the ability for users to choose whether to use Norwegian or English element names in the XML document.

To address this, we can define a substitutionGroup in the XML schema. First, we declare the main element, and then we declare the secondary elements, which can declare that they can substitute for the main element.

In the above example, the "name" element is the main element, while the "navn" element can substitute for the "name" element.

Here is a fragment of an XML schema:

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

Or like this:


Preventing Element Substitution

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

Here is a fragment of an XML schema:

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

But the following document is no longer valid:


Using substitutionGroup

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

Note that all elements in the substitutionGroup (both the main and substitute elements) must be declared as global elements, otherwise it will not work!


What are Global Elements?

Global elements are direct children of the "schema" element! Local elements are those nested within other elements.

❮ Schema Intro El Complextype ❯