How to Import an XSD File
If most of the schema information you need is already contained within another XSD file, there is no reason to duplicate all that information into another file. Doing so is a recipe for disaster because by spreading XSD Schema information out across multiple files, you are only creating a nightmare situation should your XSD Schema ever need to be altered. Instead, you should use the "xsd:import" and "xsi:schemaLocation" tags in your XSD documents to import XSD files into another other XSD documents that require the same information. This ensures that when the time comes to update one of your documents, the changes will propagate out to any other documents automatically.
Instructions
-
-
1
Open your new XSD file in a text editor. For the tutorial, the following very simple XSD document will be used:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="phone" type="xsd:string"/>
<xsd:element name="address" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
As you can see this XSD defines the structure of a database of persons, each with their own name, phone number and address. However, some of the details of the implementation are less than desirable: surely there is a better data definition out there for names, addresses, and phone numbers than the simple string. Ideally, our XSD document should be able to spot obviously invalid "phone numbers" like "12" or, even worse, something completely nonsensical like "I don't feel like giving you my phone number."
-
2
Add the following line to your XSD file, just below the "xsd:schema" line:
<xsd:import namespace="kevinwalkersNS" schemaLocation="phoneNumber.xsd"/>
<xsd:import namespace="kevinwalkersNS" schemaLocation="address.xsd"/>
<xsd:import namespace="kevinwalkersNS" schemaLocation="englishNames.xsd"/>
Now, you have imported three XSD files: one that defines a "phoneNumber" data type, another for an "address" data type, and a third for an "englishNames" data type. Each of them is in a name space named "kevinwalkersNS," and this prevents conflicts with other documents that may have similar variable names within them.
-
-
3
Edit the variable types of your elements that use the XSD documents you imported to use the schema contained within. In this case, the "xsd:string" should be replaced like so:
<xsd:element name="name" type="kevinwalkersNS:englishNames"/>
<xsd:element name="phone" type="kevinwalkersNS:phoneNumber"/>
<xsd:element name="address" type="kevinwalkersNS:address"/>
-
1