XML Schema / identity and structure

XSD Element Reference vs. Type Definition

Element references reuse an element declaration. Type references reuse content rules. That distinction controls names, substitution behavior, scope and future extension.

Updated2026-07-21
ScopeXSD element and type system

An XSD element reference and a type assignment can produce instance fragments that look similar, but they express different contracts. The element declaration defines a named information item. The type defines the values or child structure permitted for that item.

Element identity versus content structure

Given a global element customer, ref="tns:customer" places that same declared element in another content model. Given a named type CustomerType, type="tns:CustomerType" lets a declaration with any appropriate name use the structure.

<xs:element name="customer" type="tns:CustomerType"/>

<xs:complexType name="OrderType">
  <xs:sequence>
    <xs:element ref="tns:customer"/>
    <xs:element name="billingContact" type="tns:CustomerType"/>
  </xs:sequence>
</xs:complexType>

The first particle reuses the global customer element. The second declares a distinct local element named billingContact whose structure happens to be CustomerType.

When an element reference is the right tool

Use an element reference when the element name and declaration identity are part of the reusable contract. Common examples include shared header elements, elements used in several message roots and declarations that belong to a substitution group.

The cost is coupling. Every reference depends on the global declaration. Changing its type, nillability, default or substitution relationships may affect every model that includes it.

When a named type is the better abstraction

A named type captures reusable structure without forcing one element name. This is often the cleaner choice when different business roles share fields. A shipping address and billing address can both use AddressType without pretending they are the same element.

Named types also make derivation possible. A specialized type can extend or restrict a base type where the XSD design and consumer tooling support that strategy. Derivation should be intentional because code generators differ in how they map complex inheritance.

Decision table

RequirementPreferReason
Reuse the same element name and identityrefReuses the global declaration.
Reuse structure under different namesNamed typeSeparates content from element identity.
Structure is used onceAnonymous typeKeeps the contract local.
Support substitution groupsGlobal elementSubstitution membership is element-based.
Provide an extension pointNamed typeTypes can participate in derivation.

Worked design examples

Shared address structure

shippingAddress and billingAddress have different roles but the same fields. Two local elements using a global AddressType preserve those distinct names while eliminating duplicated structure.

Shared message header element

If every message must contain the exact same messageHeader element, a global declaration referenced from each message type expresses that identity directly. Consumers can address it consistently by namespace and local name.

Reuse an element when identity is shared. Reuse a type when only structure is shared.

The broader consequences are covered in Global vs. Local XML Schema Design.

Query set / FAQ

Frequently asked questions

01What is the difference between an XSD element and type?

An element declaration associates a name with content in an XML instance. A type defines the allowed value or content structure and can be assigned to one or more element or attribute declarations.

02What does ref mean on xs:element?

The ref attribute points to an existing global element declaration by QName. The referencing particle reuses the element name, type and identity rather than declaring a new local element.

03Can an XSD element have both name and ref?

No. A particle that uses ref refers to a global declaration and does not declare a separate name. Attributes allowed alongside ref are limited by the XSD version and context.

04Why use a named complex type?

A named complex type lets several elements share the same structure while retaining different element names. It also creates an explicit extension and restriction point.

05What is an anonymous XSD type?

An anonymous type is declared inside an element or attribute and has no QName. It is appropriate for structure that has no independent reuse or extension requirement.

06Can two elements use the same XSD type?

Yes. Multiple element declarations can specify the same named type. Their element names and substitution identity remain distinct even though their content structure is shared.

07Is an element reference the same as a type reference?

No. An element reference reuses the global element declaration itself. Assigning a named type creates or uses a separate element declaration with shared content rules.

08When should an element be global?

Use a global element when it is a document root, must be referenced from multiple content models, participates in substitution groups or represents stable element identity.