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:

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:

Key Advantages in Schema Design

  1. Namespace Isolation and Governance: Different teams or standards organizations can independently manage their own schemas without naming conflicts.
  2. Reusability of Industry Standards: Common schemas—such as Dublin Core, XML Digital Signatures, or OASIS standards—can be imported directly into custom enterprise schemas.
  3. Decoupled Architecture: Large, complex data models can be broken down into discrete domain models that interact only through explicitly imported types.