XML technologies / XML Schema / Referencing schemas and schema data types in XML documents / Linking schemas to instance documents
(Excerpt from “XML Schema” by Eric van der Vlist)
The first useful information for a schema processor is a hint about where to find schemas relevant to the instance document. This resembles the SYSTEM identifier of an XML doctype declaration, but with important differences. First, one schema may not suffice to describe a document, since each schema can describe only one namespace, or its absence, and schemas may be assembled in the instance document. Second, the locations supplied in instance documents are only hints and can be overridden by the user or the processor.
The tight connection between an XML document and its DTD has prompted much debate in the XML community. Many developers remember Netscape restructuring its website: the DTD address for RSS 0.91, "http://my.netscape.com/publish/formats/rss-0.91.dtd", suddenly returned a 404 error, breaking hundreds of applications. Another reason for loose links between instances and schemas is to allow different schemas according to local processing rules. For example, a supplier receiving an order as XML may have special rules for checking it against its own schema.
For these reasons, the Recommendation says that a processor finding such information should try the specified locations, but may receive other instructions from the calling application or user. Without it, the calling application may still direct the processor to specific locations. If neither supplies information, the processor may search by any means. Listed methods include retrieving the resource at the namespace URI to see whether a schema is published there, or trying techniques such as RDDL or catalogue systems.
W3C XML Schema defines two attributes: one for a list of schema locations associated with target namespaces, and one for the location of a schema without a target namespace. The attribute for the latter case is xsi:noNamespaceSchemaLocation. Its value is a URI pointing to the schema. Although usable without declaration on any element of any instance document, it must be found before the processor has to validate any element or attribute: at the latest, at the final point of the first element without a namespace encountered in the document. Its scope is the entire document, and its value cannot be redefined.
In practice, the attribute xsi:noNamespaceSchemaLocation is often placed on the document element. A schema named first.xsd can be found in the same directory as this chapter’s namespace-free instance document:
<library xsi:noNamespaceSchemaLocation="first.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
.../...
</library>Schemas with target namespaces are referenced through URI lists in the attribute xsi:schemaLocation. More precisely, these are lists of URI pairs: the first URI identifies the target namespace, the second the location of a schema for it. The same positioning rule as for xsi:noNamespaceSchemaLocation applies: for each target namespace, supply the schema location before the processor needs to start work.
To illustrate the attribute xsi:schemaLocation, consider a simplified version of the two-namespace example in Controlling namespaces. The instance document looks as follows, without any xsi:schemaLocation:
<?xml version="1.0"?>
<book id="b3810518883" xmlns="http://dyomedea.com/ns/library" xmlns:mkt="http://dyomedea.com/ns/library/mkt">
<title>
Auf den Hund gekommen
</title>
<author>
Charles M. Schulz
</author>
<mkt:cover>
Paperback
</mkt:cover>
<mkt:pages>
128
</mkt:pages>
</book>We use an open schema for the main namespace, allowing arbitrary elements from other namespaces, for example:
<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:lib="http://dyomedea.com/ns/library" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:token"/>
<xs:element name="author" type="xs:token" maxOccurs="unbounded"/>
<xs:any namespace="##other" processContents="lax" minOccurs="0"maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
</xs:complexType>
</xs:element>
</xs:schema>We also write a schema for the two elements belonging to our marketing department’s namespace:
<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library/mkt" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="cover" type="xs:NMTOKEN"/>
<xs:element name="pages" type="xs:nonNegativeInteger"/>
</xs:schema>This example deliberately uses two unconnected schemas for two namespaces. The library does not reference the marketing namespace or vice versa. Several outcomes are possible depending on the hints supplied. If we validate the instance without an attribute xsi:schemaLocation and without information from the application or invocation, the validator must find a schema itself. Depending on its implementation, it may try the document element’s namespace URI, attempting to load any resource found there. Here that URI is "http://dyomedea.com/ns/library". If no schema is there, it cannot determine whether the document is valid. Alternatively, it may try to load an RDDL document there and hope to find a schema reference.
Usually, however, the instance author will kindly provide the library namespace’s schema location, for example:
<?xml version="1.0"?>
<book xsi:schemaLocation="http://dyomedea.com/ns/library library.xsd" id="b3810518883" xmlns="http://dyomedea.com/ns/library" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mkt="http://dyomedea.com/ns/library/mkt">
<title>
Auf den Hund gekommen
</title>
<author>
Charles M. Schulz
</author>
<mkt:cover>
Paperback
</mkt:cover>
<mkt:pages>
128
</mkt:pages>
</book>We cannot choose where to put xsi:schemaLocation: this information is needed to validate the document element, so the attribute must be on that element. It contains one pair of values separated by whitespace:
"http://dyomedea.com/ns/library library.xsd"
As noted, the first value identifies the target namespace and the second the schema location. With this information, the processor can read the schema and start validating the instance. Eventually it encounters the marketing namespace, matching the wildcard xs:any. Its attribute processContents requests validation where possible, so the processor may again search for a schema by accessing the namespace URI. If it finds one, it validates the marketing elements; otherwise it considers them valid because the attribute processContents is set to “lax”.
To improve the chance of finding a schema for the marketing library, we can also specify its location in an attribute xsi:schemaLocation. The place to supply this information is the first element using that namespace, for example:
<?xml version="1.0"?>
<book id="b3810518883" xsi:schemaLocation="http://dyomedea.com/ns/library library.xsd" xmlns="http://dyomedea.com/ns/library" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mkt="http://dyomedea.com/ns/library/mkt">
<title>
Auf den Hund gekommen
</title>
<author>
Charles M. Schulz
</author>
<mkt:cover xsi:schemaLocation="http://dyomedea.com/ns/library/mkt marketing.xsd">
Paperback
</mkt:cover>
<mkt:pages>
128
</mkt:pages>
</book>The processor now has all the hints needed to fetch schemas for both namespaces and should fully validate the marketing elements. Alternatively, all schema-location hints can be placed in the same attribute xsi:schemaLocation:
<?xml version="1.0"?>
<book id="b3810518883" xsi:schemaLocation="http://dyomedea.com/ns/library library.xsd http://dyomedea.com/ns/library/mkt marketing.xsd" xmlns="http://dyomedea.com/ns/library" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mkt="http://dyomedea.com/ns/library/mkt">
<title>
Auf den Hund gekommen
</title>
<author>
Charles M. Schulz
</author>
<mkt:cover>
Paperback
</mkt:cover>
<mkt:pages>
128
</mkt:pages>
</book>These examples used relative URIs to locate schemas. This works well only if schemas move together with instance documents. Absolute URIs are preferable in many cases; a mechanism such as XML Catalogs can map them back to local resources. XML Catalogs is an OASIS specification implemented by a growing number of tools.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML Schema” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top