Using Multiple Documents

As an application grows and becomes more complex, it is important to take steps to maintain readability and extensibility. Things like separating a large schema into multiple documents, importing declarations from external schemas, and deriving new types from existing types are all typical tasks that will face designers of real-world schemas.

Just as large computer programs are separated into multiple physical source files, large schemas can be separated into smaller, self-contained schema documents. Although a single large schema could be arbitrarily separated into multiple smaller documents, taking the time to group related declarations into reusable modules can simplify future schema development.

There are three mechanisms that include declarations from external schemas for use within a given schema: xs:include, xs:redefine, and xs:import. The next three sections will discuss the differences between these methods and when and where they should be used.

Including external declarations

The xs:include element is the most straightforward way to bring content from an external schema into your own schema. To demonstrate how xs:include might be used, Example 17-12 shows a new schema document called physical-address.xsd that contains a declaration for a new complex type called physicalAddressType.

Example 17-12. physical-address.xsd
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema" targetNamespace="http://namespaces.oreilly.com/xmlnut/address" xmlns:addr="http://namespaces.oreilly.com/xmlnut/address" ...

Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.