XSD Element Substitution (Element Substitution)
With XML Schema, one element can be substituted for another element.
Element Substitution
Let's illustrate with an example: our users come from the UK and Norway. We want to give users the ability to choose whether to use Norwegian or English element names in the XML document.
To solve this problem, we can define a in the XML schema substitutionGroup. First, we declare the main element, then we declare the secondary elements, which can declare that they are able to substitute for the main element.
In the example above, the "name" element is the main element, and the "navn" element can substitute for the "name" element.
Look at a fragment of an XML schema:
A valid XML document would look like this (according to the schema above):
or like this:
Blocking Element Substitution
To prevent other elements from substituting for a specified element, use the block attribute:
Look at a fragment of an XML schema:
A valid XML document should look like this (according to the schema above):
But the following document is no longer valid:
Using substitutionGroup
The type of a substitutable element must be the same as, or derived from, the type of the main element. If the type of the substitutable element is the same as the type of the main element, then you do not have to specify the type of the substitutable element.
Please note that all elements in a substitutionGroup (both the main element and the substitutable elements) must be declared as global elements, otherwise it will not work!
What are Global Elements?
Global elements are the direct child elements of the "schema" element! Local elements are elements nested inside other elements.