elementFormDefault qualified vs unqualified in XSD
The elementFormDefault attribute in an XML Schema (XSD)
determines whether locally declared elements in an XML instance document
must be qualified with a namespace. Choosing between
qualified and unqualified controls whether
child elements inherit the schema’s targetNamespace or
exist in no namespace at all, directly impacting how instance documents
must be structured and parsed.
Understanding elementFormDefault
In an XML Schema, the elementFormDefault attribute is
set on the root <xs:schema> element. It defines the
default namespace qualification rules for all local elements (elements
defined inside complex types) within that schema. Global elements
(direct children of <xs:schema>) are always qualified
by the schema’s targetNamespace, regardless of this
setting.
The attribute accepts two values: * qualified *
unqualified (default if omitted)
elementFormDefault=“qualified”
When elementFormDefault="qualified", every locally
declared element is associated with the schema’s
targetNamespace.
In the corresponding XML instance documents, all child elements must
belong to the target namespace. This is typically handled by declaring a
default namespace (xmlns="...") on the root element or
using namespace prefixes (<prefix:element>) on all
child tags.
XSD Example
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/orders"
xmlns="http://example.com/orders"
elementFormDefault="qualified">
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderID" type="xs:string"/>
<xs:element name="Customer" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>Valid Instance XML (Default Namespace)
<Order xmlns="http://example.com/orders">
<OrderID>12345</OrderID>
<Customer>Jane Doe</Customer>
</Order>Because the default namespace is defined on
<Order>, the child elements
<OrderID> and <Customer>
automatically belong to http://example.com/orders.
elementFormDefault=“unqualified”
When elementFormDefault="unqualified", locally declared
elements do not belong to the targetNamespace. Instead,
they reside in no namespace (an empty namespace).
Only the global root element belongs to the
targetNamespace. In instance documents, child elements must
not have a namespace prefix and must not inherit the default
namespace.
XSD Example
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/orders"
xmlns="http://example.com/orders"
elementFormDefault="unqualified">
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderID" type="xs:string"/>
<xs:element name="Customer" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>Valid Instance XML (Prefix Notation)
<ord:Order xmlns:ord="http://example.com/orders">
<OrderID>12345</OrderID>
<Customer>Jane Doe</Customer>
</ord:Order>Here, ord:Order is in the namespace, but
<OrderID> and <Customer> are
unqualified (no namespace).
Summary of Differences
| Feature | qualified |
unqualified (Default) |
|---|---|---|
| Local Elements Namespace | Belong to
targetNamespace |
Belong to no namespace |
| Instance XML Authoring | Clean with default namespaces
(xmlns="...") |
Requires prefixing only root or resetting namespace |
| Schema Evolution | Refactoring local elements to global does not break XML documents | Refactoring local elements to global changes their namespace |
| Industry Practice | Widely recommended as standard best practice | Common in older WSDLs and legacy systems |
Setting elementFormDefault="qualified" is generally
preferred in modern system design because it allows clean, prefix-free
XML documents using default namespaces and prevents naming conflicts
across multiple schemas.