How defaultAttributes Works in XML Schema 1.1
This article explains how the defaultAttributes
attribute in XML Schema 1.1 enables schema authors to apply a
standardized set of attributes globally across all complex types. It
details the setup required on the root schema element, demonstrates how
global attribute groups are linked, and outlines how individual complex
types can opt out using the defaultAttributesApply
attribute.
Overview of defaultAttributes
In XML Schema 1.0, adding common metadata attributes—such as
id, timestamp, or xml:lang—to
multiple elements required manually referencing an
xs:attributeGroup inside every single
xs:complexType.
XML Schema 1.1 introduced the defaultAttributes
attribute on the <xs:schema> element to eliminate
this repetition. When declared, it designates a global attribute group
whose attributes are automatically injected into every complex type
defined within that schema document.
How to Configure defaultAttributes
Applying attributes globally involves two primary steps: defining the
global attribute group and referencing it at the root
<xs:schema> element.
1. Define the Global Attribute Group
First, create an <xs:attributeGroup> at the top
level of the schema containing the attributes you want to apply
universally.
<xs:attributeGroup name="CommonMetadata">
<xs:attribute name="id" type="xs:ID" use="optional"/>
<xs:attribute name="lastModified" type="xs:dateTime" use="optional"/>
</xs:attributeGroup>2. Reference the Group on the Schema Element
Next, set the defaultAttributes attribute on the
<xs:schema> root element, assigning it the name of
your attribute group.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
defaultAttributes="CommonMetadata">Once declared, any <xs:complexType> defined in
this schema document automatically inherits the id and
lastModified attributes without explicit
<xs:attributeGroup ref="..."/> declarations.
Complete Schema Example
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/ns"
xmlns="http://example.com/ns"
elementFormDefault="qualified"
defaultAttributes="CommonMetadata">
<!-- Global Attribute Group -->
<xs:attributeGroup name="CommonMetadata">
<xs:attribute name="id" type="xs:ID" use="optional"/>
<xs:attribute name="status" type="xs:string" use="optional"/>
</xs:attributeGroup>
<!-- Inherits CommonMetadata automatically -->
<xs:complexType name="UserType">
<xs:sequence>
<xs:element name="username" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<!-- Inherits CommonMetadata automatically -->
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:schema>In an XML instance validating against this schema, both
<User> and <Product> elements can
use id and status attributes.
Opting Out with defaultAttributesApply
If a specific complex type should not inherit the default attributes,
you can disable inheritance using the
defaultAttributesApply attribute on that specific
<xs:complexType>.
<xs:complexType name="StrictDataType" defaultAttributesApply="false">
<xs:sequence>
<xs:element name="rawPayload" type="xs:string"/>
</xs:sequence>
</xs:complexType>Setting defaultAttributesApply="false" prevents the
global attributes from being added to StrictDataType.
Scope and Behavior Rules
- Schema-Document Level: The
defaultAttributesmechanism applies per schema document. Included or imported schemas must explicitly declare their owndefaultAttributesif they require the same behavior. - Complex Types Only: This mechanism only applies to
<xs:complexType>definitions;<xs:simpleType>definitions cannot carry attributes and are unaffected. - Extension and Restriction: When deriving complex types by extension or restriction, default attribute application rules follow standard XSD 1.1 inheritance behaviors unless explicitly suppressed.