Skip Navigation LinksHome : CBC Classes : XML : Schemas : Namespaces

CS216 XML - More About Namespaces and Schemas

Now that you understand how to create schemas and define elements and attributes, the time has almost come to combine elements from different schemas into the same file. Before we do that we'll look at how to link an XML to a schema file using a namespace.

(If you need to review the basics of namespaces take another look at Getting Started )

  1. The XML file
  2. Declaring the Namespace in the Schema File

The XML file

Up to this point we've been linking an XML file to it's schema using the default namespace and the noNamespaceSchemaLocation attribute. Now we need to link using a namespace and prefix, and the schemaLocation attribute.

As an example, let's look at the following XML file:

1. <?xml version="1.0" encoding="utf-8"?>
2. <myOwnPrefix:movies xmlns:xsi=""
3.    xmlns:myOwnPrefix="http://myNamespace"
4.    xsi:schemaLocation="http://myNamespace  mySchema.xsd" 
5.    xsi:noNamespaceSchemaLocation="mySchema.xsd">
6.      Movie Name
7. </myOwnPrefix:movies>

We still need the xmlns:xsi="" attribute in line 2 because it contains the definitions for the schemaLocation attribute that we use in line 4.

We're replacing the noNamespaceSchemaLocation attribute in line 5 with the schemaLocation attribute in line 4. This attribute requires two values as arguments. The first is the namespace, in this case http://myNamespace; and the second is the name of the .xsd file, which in this example is mySchema.xsd.

Notice that even though the namespace http://myNamespace starts with http:// like a URL, it's not a URL. It's just a string of characters. In fact, the http:// isn't required, we could call it "Bob", "Ralph" or "powerstrip" if we wanted. The only trick is that we are going to use the namespace name in three places, and it has to be exactly the same characters in all three places or the XML file and .xsd schema file won't be linked.

The next step is to define the prefix we want to use with the namespace. This is done in line 3, with the same xmlns attribute we've been using all along. The only difference is that instead of defining the xsi prefix for the W3C's schema, we're defining the prefix for our own namespace. So what line 3 is saying is that we want to use the prefix myPrefix with the namespace http://myNamespace. Note the namespace in this line must exactly match the namespace in line 4.

The last thing to do in the XML file is to add the prefix myPrefix to any and all of the elements that are defined in our schema file. This is done in lines 2 and 4, on the movies element, which is now referred to as myPrefix:movies.

These are all the changes that need to be made in the XML file, but there's still one change that needs to be made in the .xsd schema file.

Declaring the Namespace in the Schema File

If you're going to link to a schema using a namespace, then the schema needs to know about it as well. It's done usingthe targetNamespace attribute, which is shown in line 3 of the following example. Just make sure that the namespace you use is exactly the same as the namespace in the XML file.

1. <?xml version="1.0" encoding="utf-8"?>
2. <xsd:schema xmlns:xsd=""
3.      targetNamespace="http://tony">
4.   <xsd:element name="movies" type="xsd:string" />
5. </xsd:schema>