Understanding xs:import in XML Schemas
The xs:import element in XML Schema Definition (XSD)
enables a schema to reference and reuse components defined in an
external schema that belongs to a different target namespace. By acting
as a bridge between distinct namespaces, xs:import allows
developers to build modular, maintainable, and standardized schema
architectures where components from separate domains can be combined and
validated seamlessly within a single XML document.
The Core Function of xs:import
In XML Schema, an XML document often requires elements and data types
governed by different vocabularies or organizational domains. The
primary role of xs:import is to make components from a
foreign namespace accessible in the importing schema. Without
xs:import, a schema cannot validate or reference types,
elements, or attributes defined outside of its own declared
targetNamespace.
Key Attributes of the xs:import Element
The xs:import element utilizes two primary attributes to
locate and incorporate external definitions:
namespace: Specifies the target namespace URI of the schema being imported. This attribute is required if the imported components belong to a defined namespace.schemaLocation: Provides the URI or relative file path to the actual.xsdfile containing the definitions. While optional according to the W3C specification, providing it is standard practice so schema processors can resolve and validate the external definitions.
Example Syntax
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/orders"
xmlns:ord="http://example.com/orders"
xmlns:cust="http://example.com/customers"
elementFormDefault="qualified">
<!-- Importing an external schema with a different namespace -->
<xs:import namespace="http://example.com/customers"
schemaLocation="customers.xsd"/>
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderID" type="xs:string"/>
<!-- Reference to a type from the imported namespace -->
<xs:element name="CustomerDetails" type="cust:CustomerType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>xs:import vs. xs:include
The distinction between xs:import and
xs:include centers entirely on namespace compatibility:
xs:importis used exclusively when combining schemas that have different target namespaces, or when a namespaced schema imports a schema with no target namespace.xs:includeis used to merge schema documents that share the exact same target namespace (or lack a target namespace altogether).
Key Advantages in Schema Design
- Namespace Isolation and Governance: Different teams or standards organizations can independently manage their own schemas without naming conflicts.
- Reusability of Industry Standards: Common schemas—such as Dublin Core, XML Digital Signatures, or OASIS standards—can be imported directly into custom enterprise schemas.
- Decoupled Architecture: Large, complex data models can be broken down into discrete domain models that interact only through explicitly imported types.