XML Schema final Attribute and Type Extension

The final attribute in XML Schema Definition (XSD) is a mechanism used to control inheritance and prevent further derivation of complex types. By defining this attribute on a complex type definition, schema authors can explicitly prohibit derived types from extending, restricting, or modifying the base structure. This article explains the purpose of the final attribute, its valid values, and specifically how it restricts complex type extension to ensure strict schema contracts and data validation integrity.

What is the final Attribute in XML Schema?

In XML Schema, complex types can normally serve as base types that other types derive from using <xs:extension> or <xs:restriction>. The final attribute is placed within an <xs:complexType> or <xs:element> declaration to seal or restrict that definition from further modification.

The final attribute accepts the following values: * extension: Prevents other types from deriving from this type via extension. * restriction: Prevents other types from deriving from this type via restriction. * #all: Prohibits both extension and restriction completely. * List value ("extension restriction"): Explicitly lists allowed or prohibited derivations via a whitespace-separated list.

Restricting Complex Type Extension

Extension in XML Schema allows a child type to inherit the elements and attributes of a base complex type and append additional elements or attributes to it. When an XML Schema author sets final="extension" (or final="#all") on a base complex type, the schema processor disallows any new <xs:complexType> from using an <xs:extension base="..."> referencing that base type.

If a developer attempts to extend a complex type marked as final for extension, the XML Schema processor will raise a schema-validation error, rejecting the invalid schema definition.

Example of Prohibited Extension

Consider the following base type definition:

<xs:complexType name="AddressType" final="extension">
    <xs:sequence>
        <xs:element name="Street" type="xs:string"/>
        <xs:element name="City" type="xs:string"/>
        <xs:element name="PostalCode" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

If another type attempts to extend AddressType, as shown below:

<!-- This will cause a schema validation error -->
<xs:complexType name="ExtendedAddressType">
    <xs:complexContent>
        <xs:extension base="AddressType">
            <xs:sequence>
                <xs:element name="Country" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

Because AddressType specifies final="extension", the schema validator immediately flags ExtendedAddressType as invalid.

Schema-Wide Default Behavior

Schema authors can also set the finalDefault attribute on the root <xs:schema> element. Setting finalDefault="extension" applies the restriction globally to all complex types declared within that schema file, unless an individual type explicitly overrides the setting with an empty final="" attribute.

Key Benefits of Restricting Extensions