XML Schema block Attribute Explained

The XML Schema (XSD) block attribute is a constraint mechanism used to prevent element substitution and runtime type replacement within XML instance documents. While XML Schema allows for polymorphic structures through derived types and substitution groups, the block attribute gives schema authors fine-grained control to restrict these behaviors. By specifying values such as extension, restriction, substitution, or #all, schema designers can allow types to be derived in the schema definition while prohibiting their actual use in XML payloads.

Understanding the block Attribute

In XML Schema, polymorphism allows an XML instance document to use a more specific type than declared in the schema (via the xsi:type attribute) or replace an element with a member of its substitution group. The block attribute suppresses this polymorphism at validation time.

The block attribute can be applied in three locations: * <xs:element>: Controls whether the specific element can be replaced by a member of a substitution group or accept an xsi:type override. * <xs:complexType>: Controls whether instances of this type can be substituted with derived types via xsi:type. * <xs:schema> (as blockDefault): Sets the default blocking behavior globally for all elements and types within the schema.

Permitted Values for block

The block attribute accepts a space-separated list of keywords or specific single values:

Preventing Type Derivation with xsi:type

When a type is derived by extension or restriction, XML documents normally use the xsi:type attribute to reference the derived type:

<!-- Base Type Definition -->
<xs:complexType name="AddressType" block="extension">
    <xs:sequence>
        <xs:element name="street" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

<!-- Derived Type Definition -->
<xs:complexType name="USAddressType">
    <xs:complexContent>
        <xs:extension base="AddressType">
            <xs:sequence>
                <xs:element name="state" type="xs:string"/>
                <xs:element name="zip" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

Because AddressType specifies block="extension", the schema allows USAddressType to exist in the schema, but the following XML instance will fail validation:

<!-- This will cause a validation error -->
<billTo xsi:type="USAddressType">
    <street>123 Main St</street>
    <city>Springfield</city>
    <state>IL</state>
    <zip>62701</zip>
</billTo>

The XML processor checks the block attribute on the base type. Upon detecting extension, it rejects the use of USAddressType via xsi:type.

Preventing Element Substitution

Element substitution groups allow one element to replace another in an instance document. The block attribute can disable this on the head element:

<!-- Head Element Definition -->
<xs:element name="publication" type="PublicationType" block="substitution"/>

<!-- Substituting Element Definition -->
<xs:element name="book" type="BookType" substitutionGroup="publication"/>

In an XML document expecting <publication>, replacing it with <book> is disallowed because block="substitution" is set on the head element declaration.

block vs. final Attributes

It is essential to distinguish between block and final: