ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index
Contents > Substitutions

Substitutions

Abstract element, substitutionGroup

The schema below defines an abstract element named "myAbstract" (attribute "abstract" is set to "true"). This element cannot appear in the document (because it is abstract). Then we define elements "odd" and "even", which can substitute the "myAbstract" element (see the attribute "substitutionGroup").

Although Relax NG does not contain such features, we can achieve the same effect with a Relax NG schema below.

...
Attribute block

The schema below defines an abstract element named "myAbstract" (attribute "abstract" is set to "true"). using the attribute "block" set to "substitution", we forbid all substitutions. I am not sure, if there can be any valid document :-).

...
Attribute block set to "restriction"

The schema below defines three elements: "C", "D", "E". The elements "D" and "E" have the attribute "substitutionGroup" set to "C" (so they should be able to substitute the element "C"). But only element "E" can, because the type of element "D" is derived by a restriction from the type "AAA" and this would be in conflict with the "block" attribute of element "C" (it has value "restriction"). 3.3.6 Constraints on Element Declaration Schema Components; Schema Component Constraint: Substitution Group OK (Transitive)

...
Attribute block set to "extension"

The schema below defines three elements: "C", "D", "E". The elements "D" and "E" have the attribute "substitutionGroup" set to "C" (so they should be able to substitute the element "C"). But only element "E" can, because the type of element "D" is derived by an extension from the type "AAA" and this would be in conflict with the "block" attribute of element "C" (it has value "extension"). 3.3.6 Constraints on Element Declaration Schema Components; Schema Component Constraint: Substitution Group OK (Transitive)

...
XML Schema - blockDefault attribute

The "blockDefault" attribute is set to "#all" (thus forbidding substitution, extension, restriction), the document is not valid.

The "blockDefault" attribute is set to "restriction". Because the type of element "F" is derived by a restriction from the type "AAA" (type "AAA" is the type of the substituted C element) and this would be in conflict with the "blockDefault" attribute, we can't use the element "F".

The "blockDefault" attribute is set to "extension". Because the type of element "D" is derived by an extension from the type "AAA" (type "AAA" is the type of the substituted C element) and this would be in conflict with the "blockDefault" attribute, we can't use the element "D".

...
Combining block and blockDefault attributes

The values of block and blockDefault attributes were combined and thus we can use the elements "C" or "E" only. (The type of "D" is derived by an extension from the type "AAA" and the type of "F" is derived by a restriction from the type "AAA" - both cases are in conflict with the "block" or "blockDefault" attributes).

...
Attribute block and xsi:type

The "block" attribute can be useful to prevent overriding of our schema with "xsi:type" attribute in the document.

...
Abstract type

The schema below defines an abstract complexType named "myAbstractType" (attribute "abstract" is set to "true"). The type "AAA" is an extension of this type - we add the requirement that attribute "bbb" must be present. Element declaration cannot use abstract type.

Although Relax NG does not contain such features, we can achieve the same effect with the Relax NG schema below.

...