|
|
The Internet is filled with XML documents. Even inside
of a company or a department, many people create XML or XML-types of
documents on a daily basis. One of the things that these many documents
share is that they use same words or names, by coincidence or without any
intention. The ultimate problem is that, when two XML documents are accessed
in the same application, if those two documents use the same name, there
could be a conflict of names.
|
A namespace is a technique of qualifying a name so
that it would not conflict with another name. Normally, you should already
be familiar with namespaces because they are used in C#. For example, the
following program will not compile:
using System;
using System.Windows.Forms;
public class Employee
{
public string FullName;
}
public class Employee
{
public string FullName;
}
public class Namespaces : Form
{
public Namespaces()
{
InitializeComponent();
Employee fullTime = new Employee();
Employee seasonal = new Employee();
}
private void InitializeComponent()
{
}
}
public class Exercise
{
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Namespaces());
return 0;
}
}
The reason is that the names of the Employees classes
conflict with each other. But, if each is included in its own namespace,
when each is accessed out the class, its name can be qualified and the
conflict would be avoided. Here is an example:
namespace Corporate
{
public class Employee
{
public string FullName;
}
}
namespace Research
{
public class Employee
{
public string FullName;
}
}
public class Namespaces : Form
{
public Namespaces()
{
InitializeComponent();
Research.Employee fullTime = new Research.Employee();
Corporate.Employee seasonal = new Corporate.Employee();
}
private void InitializeComponent()
{
}
}
The concept is primarily the same in XML. You create a
namespace in your XML document so that its elements and attributes can be
accessed outside that document without worrying about name conflicts
because the names of your document will be qualified.
|
The Construction of a Namespace
|
|
In XML, a namespace is made of two parts: a namespace
name and a local name.
As you may know already, to open an XML document, you
can use a browser. For example, if you have a document named employees.xml
and that is hosted on the web site of funfunfun.com, you can open it using
http://www.funfunfun.com/employees.xml. Another web site, such as
blahblahblah.com, can have a file named employees.xml and that file can be
accessed with http://www.blahblahblah.com/employees.xml.
A namespace is created as an XML attribute. This means
that you must create an element that has a name. Here is an example:
<business></business>
The keyword to create an XML namespace is
xmlns. The formula to create a namespace is:
PrefixAttributeName | DefaultAttributeName Name
This means that you primarily have two options:
In an XML namespace, binding consists of associating
an element to a prefix. To create this binding, you must assign a value to
your namespace. The value must be a Uniform Resource Identifier (URI),
which is an address of a document that resides somewhere, on the World
Wide Web or in a computer. When it comes to XML, the URI must identify a
namespace.
There are two types of URI documents:
Normally, in order
Any of them can be used to identify a namespace.
Remember that, when creating a namespace, you have two options:
This whole formula is referred to as a namespace
binding. In this formula, xmlns and what follows it is
not considered an attribute in the traditional sense. This means that if
you try to access the collection of attributes of the tag, xmlns and its
expression will not be considered as a member of that collection.
In addition to these, there are a few rules you must
follows:
- If you decide to use a prefix:
- Don't use xmlns as your default namespace
To own a URL (such as a website), you must register a
domain name with an authority (such as Network Solutions or GoDaddy, etc).
Once you own the name, you can use it as you see fit. For example, inside
your website, you can create directories and sub-directories any way you
want. To communicate a URL to other people, you give the address. When it
comes to namespaces, you can also create your own, using a domain name you
will have registered. As an alternative, you can use namespaces that other
people have created. As mentioned above the W3C organization provides a
few namespaces already. Microsoft also provides many already created
namespaces. We will come back to them.
After creating a namespace, you can use the elements
to which it refers. When creating an XML element, precede the tag's name
with a prefix of the namespace and a colon ":". After the name of the tag,
define the namespace that contains xmlns: followed by the
prefix and assign the namespace's URL to it. Here is an example:
<?xml version="1.0" encoding="utf-8" ?>
<car:status xmlns:business="http://www.functionx.com/rent" />
If you decide to close the tag with an end tag, you
must use the tag name:prefix formula. Here is an
example:
<?xml version="1.0" encoding="utf-8" ?>
<car:status xmlns:business="http://www.functionx.com/rent"></car:status>
Remember that the xmlns expression is not an
attribute. If you want an actual attribute, you can add it. Here is an
example:
<?xml version="1.0" encoding="utf-8" ?>
<car:status xmlns:business="http://www.functionx.com/rent" available="Yes"></car:status>
In the same way, you can add as many attributes as you
want.
If you want to create a tag that has an
attribute, after the name of the tag, type the prefix you had defined for
the namespace, followed by :, followed by the attribute's name and assign
the desired value to it. Optionally, you can assign the desired value to
the attribute. If necessary, close the tag and optionally specify its
value. Here is an example:
<?xml version="1.0" encoding="utf-8" ?>
<business xmlns:CarRental="http://www.functionx.com/rent">
<car CarRental:status="Available">Ford Escort</car>
</business>
To access one of the names, you must qualify it. To do
this, type the name of the prefix, followed by :, and followed by the name
of the element.