Substitution Groups

A feature that is closely related to derived types is the substitution group . A substitution group is a collection of elements that are all interchangeable with a particular element, called the head element , within an instance document. To create a substitution group, all that is required is that an element declaration include a substitutionGroup attribute that names the head element for that group. Then, anywhere that the head element’s declaration is referenced in the schema, any member of the substitution group may also appear. Unlike derived types, it isn’t necessary to use the xsi:type attribute in an instance document to identify the type of the substituted element.

Tip

The primary restriction on substitution groups is that every element in the group must be either of the same type as or derived from the head element’s type. Declaring a numeric element and trying to add it to a substitution group based on a string element would generate an error from the schema processor. The elements must also be declared globally and in the target namespace of the schema.

Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.