The xsd
Prefix
The first thing to understand about namespaces and schemas is that there is nothing magical about the prefix xsd
. The prefix xsd
is used with the XSD schema as a means of referencing elements and attributes that are used to construct schemas for your own custom markup languages. For example, following is the namespace declaration for the etml.xsd
example schema document, which you will learn about in just a moment:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
This code shows how the prefix xsd
is used to declare the XSD schema explicitly. Now that you understand how prefixes work with explicit namespace declarations, you know that this prefix could be named anything you want. Of course, there is no reason to deviate from xsd
since it has become somewhat of a standard among XML developers, but I wanted to point out that there is nothing hardwired into XML when it comes to namespace prefixes.
Referencing Schema Documents
In addition to providing a means of referencing the schema of a schema document, namespaces also play an important role in documents that rely on an XSD schema for validation. If this sounds confusing, I think a quick explanation will clear things up. In order to identify the physical schema document for a document, you must use a special attribute and assign the location of the schema document to it. There are two attributes you can use to accomplish this task:
-
schemaLocation
Locates a schema and its associated namespace -
noNamespaceSchemaLocation
Locates a schema with no namespace
These attributes are standard attributes that are located in a namespace named http://www.w3.org/2001/XMLSchema-instance. In order to properly reference either of these attributes, you must first explicitly declare the namespace in which they are located. It is standard to use the xsi
prefix for this namespace, as the following attribute assignment shows:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
With this namespace declared, you can now use one of the schema location attributes to reference the physical schema document. Following is an example of how this task is carried out for the training log example document, which is based on the etml.xsd schema:
<trainlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="etml.xsd">
In this example the noNamespaceSchemaLocation
attribute is used because you don't care about associating the ETML schema with a namespace. If, however, you wanted to associate it with a namespace, you would use the schemaLocation
attribute instead:
<trainlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xyz.com/ns/etml etml.xsd">
Notice in the schemaLocation
attribute that two pieces of information are provided: the namespace for the schema and the location of the schema document. The schemaLocation
attribute is useful whenever you are working with a schema and you want to associate it with a namespace. It's important to understand that this sample code doesn't actually establish a schema prefix for the ETML document. Instead, it simply establishes that the etml.xsd schema document is associated with the ETML namespace. To establish prefix for the ETML tags and attributes, you must declare the ETML namespace, as shown in this code:
<trainlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:etml="http://www.xyz.com/ns/etml" xsi:schemaLocation="http://www.xyz.com/ns/etml etml.xsd">
Now the prefix etml
can be used to reference tags and attributes as part of the ETML namespace, as in <etml:distance>
.