Because developers create their own tag names in XML, you must use namespaces to define particular elements. If you are using two XML files within your application and the two files use some of the same tags, namespaces become very important. Even though the tags have the same name, they might have different meanings. For instance, compare the two XML files in Listings 1-13 and 1-14.
Listing 1-13: Book.xml
<?xml version="1.0" encoding="UTF-8" ?> <Book> <Title>Professional ASP.NET 2.0</Title> <Price>49.99</Price> <Year>2005</Year> </Book>
Now take a look at the second XML file. You should be able to see where the problem lies.
Listing 1-14: Author.xml
<?xml version="1.0" encoding="UTF-8" ?> <Author> <Title>Mr.</Title> <FirstName>Bill</FirstName> <LastName>Evjen</LastName> </Author>
A conflict exists with the <Title> tag. If you are using both these XML files, you might be able to tell the difference between the tags by just glancing at them; but computers are unable to decipher the difference between two tags with the same name.
The solution to this problem is to give the tag an identifier that enables the computer to tell the difference between the two tags. Do this is by using the XML namespace attribute, xmlns. Listing 1-15 shows how you would differentiate between these two XML files by using XML namespaces.
Listing 1-15: Revised Book.xml using an XML namespace
<?xml version="1.0" encoding="UTF-8" ?> <Book xmlns="http://www.xmlws101.com/xmlns/book"> <Title>Professional ASP.NET 2.0</Title> <Price>49.99</Price> <Year>2005</Year> </Book>
Notice that you now have added the XML namespace attribute to your root element <Book>. Now look at the second file (Listing 1-16).
Listing 1-16: Revised Author.xml using an XML namespace
<?xml version="1.0" encoding="UTF-8" ?> <Author xmlns="http://www.xmlws101.com/xmlns/author"> <Title>Mr.</Title> <FirstName>Bill</FirstName> <LastName>Evjen</LastName> </Author>
In this example, the <Author> element contains an XML namespace that uniquely identifies this XML tag and all the other tags that are contained within it. Note that you could have put an XML namespace directly in the <Title> tag if you wished. By putting the xmlns attribute in the root element, not only do you uniquely identify the root element, but you also identify all the child elements contained within the root element.
The value of the xmlns attribute is the Universal Resource Identifier (URI). It is not required that the URI be a Web site URL as shown in the example, but this is usually a good idea. The URI can be anything that you wish it to be. For example, it could just as easily be written as xmlns=“myData” or xmlns=“12345”. But with this kind of URI, you are not guaranteed any uniqueness because another URI in another file may use the same value. Therefore, it is common practice to use a URL, and this practice serves two purposes. First, it is guaranteed to be unique. A URL is unique, and using it as your URI ensures that your URI won't conflict with any other. The other advantage to using a URL as the URI is that it also identifies where the data originates.
You don't have to point to an actual file. In fact, it is usually better not to do that, but instead to use something like the following:
xmlns="http://www.xmlws101.com/[Namespace Name]"
If the XML file has an associated XSD file, another option is to point to this file. The XSD file defines the schema of the XML file.
The style of XML namespaces used thus far is referred to as a default namespace. The other type of XML namespaces that is available for your use is a qualified namespace. To understand why you need another type of XML namespace, take a look at the example in Listing 1-17.
Listing 1-17: Author.xml using multiple XML namespaces
<?xml version="1.0" encoding="UTF-8" ?> <Author xmlns="http://www.firstserver.com/xmlns/author"> <Title xmlns="http://www.secondserver.com/title">Mr.</Title> <FirstName xmlns="http://www.thirdserver.com/fn">Bill</FirstName> <LastName xmlns="http://www.thirdserver.com/ln">Evjen</LastName> </Author>
As you can see in this example, you use a number of different XML namespaces to identify your tags. First, your <Author> tag is associated with the XML namespace from the first server. The <Title> tag is associated with the second server, and the <FirstName> and <LastName> tags are associated with the third server. XML allows you to associate your tags with more than one namespace throughout your document.
The problem is that you might have hundreds or thousands of nodes within your XML document. If one of the namespaces that is repeated throughout the document changes, you have a lot of changes to make throughout the document.
Using qualified namespaces enables you to construct the XML document so that if you need to make a change to a namespace that is used throughout the document, you only have to do it in one spot. The change is reflected throughout the document. Look at Listing 1-18 to see an XML document that uses qualified namespaces.
Listing 1-18: Author.xml using qualified XML namespaces
<?xml version="1.0" encoding="UTF-8" ?> <AuthorNames:Author xmlns:AuthorNames="http://www.firstserver.com/xmlns/author" xmlns:AuthorDetails="http://www.secondserver.com/xmlns/details"> <AuthorNames:Title>Mr.</AuthorNames:Title> <AuthorNames:FirstName>Bill</AuthorNames:FirstName> <AuthorNames:LastName>Evjen</AuthorNames:LastName> <AuthorDetails:Book>Professional ASP.NET 2.0</AuthorDetails:Book> </AuthorNames:Author>
In this document, you use an explicit declaration of the namespace. Explicit declarations of namespace prefixes use attribute names beginning with xmlns: followed by the prefix. So in the first node, you have explicitly declared two namespaces that you use later in the document.
xmlns:AuthorNames="http://www.firstserver.com/xmlns/author" xmlns:AuthorDetails="http://www.secondserver.com/xmlns/details"
Notice that the declaration name follows a colon after xmlns. In this example, you declare two qualified namespaces: AuthorNames and AuthorDetails. Later, when you want to associate an element with one of these explicit declarations, you can use the shorthand (or prefix) to substitute for the full namespace. In your document, you do this by using <AuthorNames:LastName> and <AuthorDetails:Book>.