XML Schema Instance Namespace URI and Attributes

The XML Schema instance namespace provides specialized attributes intended for use directly within XML instance documents rather than in schema definition files. This article explains the standard XML Schema instance namespace URI, its common usage conventions, and the four primary attributes it defines to control validation, document-to-schema association, data typing, and null values.

The XML Schema Instance Namespace URI

The standard XML Schema instance namespace URI is:

http://www.w3.org/2001/XMLSchema-instance

In XML documents, this namespace is conventionally bound to the prefix xsi using the xmlns:xsi attribute:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

While any prefix can technically be used, xsi is universally recognized as the standard prefix across tools and validation engines.


Standard Attributes Provided by the XSI Namespace

The XML Schema instance namespace defines four standard attributes used to guide schema processors during validation.

1. xsi:schemaLocation

The xsi:schemaLocation attribute provides hints to the XML parser about where to find schema documents for specific namespaces used in the instance document.

The value consists of pairs of whitespace-separated URIs: the first is the target namespace, and the second is the URL or file path to the corresponding .xsd file. Multiple namespace-location pairs can be listed within the same attribute.

<root xmlns="http://example.com/ns"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://example.com/ns http://example.com/ns/schema.xsd">
</root>

2. xsi:noNamespaceSchemaLocation

The xsi:noNamespaceSchemaLocation attribute points to an XML Schema file when the instance document does not use a target namespace. The value is a single URI representing the location of the schema document.

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="schema.xsd">
</root>

3. xsi:type

The xsi:type attribute explicitly specifies the derived data type or complex type of an element at runtime. This is essential for supporting polymorphism and inheritance in XML schemas, allowing an element declared with a base type to instantiate a derived type.

<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:type="EmployeeType">
    <name>John Doe</name>
    <employeeId>12345</employeeId>
</person>

4. xsi:nil

The xsi:nil attribute signals that an element has a null or empty value, even if the schema requires content. For xsi:nil="true" to be valid, the element definition in the XML Schema must explicitly include nillable="true".

<birthDate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>