Default Namespaces

Contact Us or call 1-877-932-8228
Default Namespaces

Default Namespaces

Let's consider the Artist.xsd schema for a moment. The first thing to remember is that the XML schema document is itself an instance document. The xmlns attributes are used to indicate what namespaces are associated with this instance.

The xs:schema element looks like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.webucator.com/Artist" targetNamespace="http://www.webucator.com/Artist">

The xmlns:xs attribute indicates that global elements, attributes, types, and groups in the XML Schema namespace can be used in this document, but they must be qualified with the xs: prefix.

Note that you could change this prefix to bob: or jill:, though xs: and xsd: are the most commonly used for the XML Schema namespace.

The second xmlns attribute indicates that global elements, attributes, types, and groups in the Artist namespace can be used in this document without a prefix (i.e, unqualified). The Artist namespace then is the default namespace. There can only be one default namespace in an instance document.

Now let's look at the MichaelJackson.xml instance document.

<Artist BirthYear="1958" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.webucator.com/Artist" xsi:schemaLocation="http://www.webucator.com/Artist Artist.xsd">

It uses two namespaces: http://www.w3.org/2001/XMLSchema-instance, which we will discuss later, and http://www.webucator.com/Artist.

Notice that none of the elements or attributes in the document is qualified (i.e, prefixed). That is because they all belong to the Artist namespace, which is the default namespace. The sample code below shows how this same document would look without a default namespace.

Code Sample:

Namespaces/Demos/MichaelJacksonQualified.xml
<?xml version="1.0"?>
<art:Artist BirthYear="1958"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xmlns:art="http://www.webucator.com/Artist"
			xsi:schemaLocation="http://www.webucator.com/Artist Artist.xsd">
	<art:Name>
		<art:Title>Mr.</art:Title>
		<art:FirstName>Michael</art:FirstName>
		<art:LastName>Jackson</art:LastName>
	</art:Name>
</art:Artist>

Generally, it only makes sense to use qualifiers when using more than one namespace.

Next