Using Multiple Namespaces

Contact Us or call 1-877-932-8228
Using Multiple Namespaces

Using Multiple Namespaces

Often it makes sense to use multiple namespaces for a single instance document. As an example, take a look at the following document.

Code Sample:

Namespaces/Demos/TheGirlIsMine.xml
<?xml version="1.0"?>
<Song	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xmlns="http://www.webucator.com/Song"
			xmlns:art="http://www.webucator.com/Artist"
			xsi:schemaLocation="http://www.webucator.com/Song Song.xsd">
	<Title>The Girl Is Mine</Title>
	<Year>1983</Year>
	<Artists>
		<art:Artist BirthYear="1958">
			<art:Name>
				<art:Title>Mr.</art:Title>
				<art:FirstName>Michael</art:FirstName>
				<art:LastName>Jackson</art:LastName>
			</art:Name>
		</art:Artist>
		<art:Artist BirthYear="1942">
			<art:Name>
				<art:Title>Mr.</art:Title>
				<art:FirstName>Paul</art:FirstName>
				<art:LastName>McCartney</art:LastName>
			</art:Name>
		</art:Artist>
	</Artists>
</Song>

The default namespace is the Song namespace. The Artist namespace is qualified with the art: prefix. Locally declared elements (there are none) and attributes (e.g, BirthYear) are unqualified.

Let's look at Song.xsd.

Code Sample:

Namespaces/Demos/Song.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
					xmlns:art="http://www.webucator.com/Artist"
					xmlns="http://www.webucator.com/Song"
					targetNamespace="http://www.webucator.com/Song">
	<xs:import namespace="http://www.webucator.com/Artist" 
		schemaLocation="Artist.xsd"/>
	<xs:element name="Title" type="xs:string"/>
	<xs:element name="Year" type="xs:gYear"/>
	<xs:element name="Artists">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="art:Artist" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="Song">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="Title"/>
				<xs:element ref="Year"/>
				<xs:element ref="Artists"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

By importing the Artist namespace with xs:import and specifying that elements in that namespace can be referenced with the xmlns:art attribute of xs:schema, elements and attributes in the Artist namespace are accessible to this schema.

Next