XML technologies / XML Schema / XML Schema element reference / xs:import

(Excerpt from “XML Schema” by Eric van der Vlist)

Importing a W3C XML Schema for another namespace.

<xs:import
    id = xs:ID
    namespace = xs:anyURI
    schemaLocation = xs:anyURI
    {beliebige Attribute mit einem Nicht-Schema-Namensraum}
    >
    Content: (xs:annotation?)
</xs:import>

May occur within: xs:schema

Description

xs:import identifies where a W3C XML Schema validator can find definitions belonging to namespaces other than the current schema’s target namespace.

All global definitions, elements, attributes, element and attribute groups, simple and complex types, and uniqueness and key constraints from imported schemas can be referenced using a prefix defined for the relevant namespace URI.

Schema locations specified in xs:import elements are only hints to validators and may be omitted. In that case, the recommendation leaves schema identification to the instance, application or user through the mechanisms in “Layer 3: Schema Document Access and Web Interoperability”.

xs:import can also import components without target namespaces into schemas with target namespaces.

Restrictions

xs:import must not import component definitions from the target namespace; two other elements serve this purpose, see xs:include and xs:redefine.

Only global component definitions from imported schemas can be referenced; local definitions can never be referenced.

Scope rules described for element xs:keyref also apply to constraint references across namespaces. The root element of the keyref constraint must be an ancestor of, or the same element as, the uniqueness or key constraint’s root.

Example

<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ppl="http://dyomedea.com/ns/people" xmlns:lib="http://dyomedea.com/ns/library">
    <xs:import namespace="http://dyomedea.com/ns/people" schemaLocation="simple-2-ns-ppl.xsd"/>
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" type="lib:bookType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="bookType">
        <xs:sequence>
            <xs:element name="isbn" type="xs:NMTOKEN"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="authors">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="ppl:person"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="characters">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="ppl:person" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID" use="required"/>
        <xs:attribute name="available" type="xs:string" use="required"/>
    </xs:complexType>
</xs:schema>

Attributes

id
Element ID according to W3C XML Schema.

namespace
Namespace URI of the imported components. If absent, components are assumed to have no namespace; otherwise it must differ from the importing schema’s target namespace.

schemaLocation
Location of the schema to import. If absent, the validator may expect this information from the application or seek it online.

   

 

 

 

back to top