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 heavily used in C#.

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 in 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. A prefix is a name by which the elements of the document will be accessed.

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.

Creating a Namespace

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:

prefix-attribute-name | default-attribute-namename

This means that you primarily have two options:

You can create a namespace to which you will specify a prefix. In this case, you type xmlns followed by a colon ":" and a prefix, which can be any valid name. Here is an example:

<business xmlns:CarRental></business>

You can create a namespace without a prefix. In this case, you simply or only use xmlns

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:

Uniform Resource Locator (URL): A URL defines an address of a document on the Internet. Its value starts with the http protocol followed by :// to indicate that an address follows. Here are examples:

Uniform Resource Name (URN): A URN is another type of URI address that
uses a different scheme. It starts with a URN or urn prefix and a colon
":". This time, the forward slashes "//" are not used. Here are examples:

Any of them can be used to identify a namespace. Remember that, when creating a namespace, you have two options:

If you decide to use a prefix, assign the URL to xmlns:Prefix. Here is an example:

<business xmlns:CarRental="http://www.abcd1234.com/rent"></business>

If you use the second option that omits a prefix, simply assign the URL to xmlns. Here is an example:

<business xmlns="http://www.abcd1234.com/"></business>

In this case, the URL is referred to as the default namespace

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 follow:

If you decide to use a prefix:

Don't use xml as the name of the prefix because it is bound to the http://www.w3.org/XML/1998/namespace URL. That is, don't create something like:

<business xmlns:xml="http://www.abcd1234.com/rent"></business>

Don't start the name of a prefix with the letters X, M, and L in any case (lowercase or uppercase)

Don't use xmlns as the name of the prefix. It is bound to http://www.w3.org/2000/xmlns. That is, don't create something like:

<business xmlns:xmlns="http://www.abcd1234.com/rent"></business>

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. The W3C organization provides a few namespaces already. Microsoft also provides many already created namespaces. We will come back to them.

Using a Namespace

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:

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:

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.

Introduction to XML Schemas

Definition

We know how to create XML elements and how to add values to them. Some values appeared as regular strings. Some appeared as number, and so on. In reality, we did not define what type each value was. In fact, we could have a child node whose value appeared as a string while the same child node in another element would appear as a number. An XML schema is a document that specifies the type of value that each element of the corresponding XML document uses. This means that an XML schema must provide at least two pieces of information: the XML document to which the schema refers and the list of elements defined by the schema.

Introduction to Creating an XML Schema

Like an XML file, an XML schema is a text-based document made of tags. You can start the document with a document type definition (DTD) that includes encoding:

<?xml version="1.0" encoding="utf-8"?>

After that line, you will create the elements of the document. The first characteristic of an XML schema document is that it must specify a namespace. The namespace is defined in a document that specifies the types of values used in the document. As seen in our introduction to namespaces, you must decide about the prefix you will use in the various tags of the document. By convention, most people use either xs, which stands for XML Schema, or xsd, which stands for XML Schema Definition. Here is an example that uses xs:

<?xml version="1.0" encoding="utf-8"?>
<xs>

As mentioned in our review of namespaces, the prefix must be followed by a colon and a name for the tag. In a typical XML schema, the first tag is named schema. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema>

As mentioned already, you must specify the namespace you will use. As mentioned in our study of namespaces, you create an attribute what uses xmlns as its prefix followed by a column and the prefix previously defined. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs>

You must assign the URL of the namespace to that attribute. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="">

After creating the document, you should (must) save it and give it a name. An XML schema file has the extension .xsd.

Built-In Schemas

There are various types of values that can be used in an XML document. Instead of defining your own types of values, many people and companies have already created XML schemas that you can simply use in your project. We refer to these as built-in XML schemas.

Among others, both the W3C consortium and Microsoft have already created XML schemas you can use. To see a list of built-in XML schemas, if you are working in Microsoft Visual Studio, first create or open an XML file. On the main menu, click XML -> Schemas... This would open the XML Schemas dialog box

Notice that, in the XML Schemas dialog box, you can add a new one by clicking Add, locating, and
then selecting the file.

To use any of these schemas, assign it to your xmlns:Prefix attribute. Here is an example:

You can use one of the built-in schemas of the XML Schemas dialog box. You can also use more than one namespace. To do this, create one schema tag and add each desired namespace. They must be separated by empty spaces. To make the document easy to read, you can write each schema on its own line. Here are examples:

As mentioned already, to create its corresponding XML schema, start a text-based document that contains a DTD and a tag that specifies the foundational namespace. Of course, this tag must be closed. Here is an example:

Between the start and the end tags, you must specify the necessary tag. Since the schema is tied to the XML tag, it must include a tag for each of the XML elements. Each tag must start with a prefix, a column, and the keyword element. This would be done as follows:

In programming languages, a data type is a word or a group of words that indicates the amount of computer memory necessary to store a value or a certain type. A value can be considered as a character, a group of characters (also called a string), a number, a date, etc. In XML, these are referred to as simple types. Consider the following XML document:

Notice that it contains only one element, that element has a value, and it does not have any child node. One of the rules of simple types is that their corresponding node cannot have a child element. In the same way, an element of simple type cannot have an attribute.

Instead of defining your own simple types, you can use a built-in schema that already has the definitions you will need and you can simply use its data types.

To specify the data type used by an element, in the XML schema and in the start-tag of the element, create an attribute whose name is type. Assign a string that contains the name of the prefix, followed by a colon, and followed by the data type. This can be done as follows:

boolean: A Boolean value is one that can be considered true (or 1) or false (or 0)

byte: If an XML element uses small signed natural numbers between -128 and 127, you can set its data type as byte.
As is the case for all numbers, the value can be represented in normal format (such as -4 or 18 or 105) or in scientific notation, which is in the form a x 10b

unsignedByte: The unsigned byte type is for elements that use small positive numbers in the range of 0 to 255

short: This type is for a a positive or negative natural number that is between -32768 and 32767

unsignedShort: This is for a small positive number

int: The int data type is used by elements whose value can be positive or negative natural number, ranging from -2147483648 to 2147483647

unsignedInt: This is appropriate for elements that use a natural positive number that is between 0 and 4294967295

integer: This type is for large natural negative or positive numbers

negativeInteger: This type is for XML elements that use negative natural numbers starting at -1 down to -∞

nonNegativeInteger: This is for negative natural numbers starting at 0 down to -∞

positiveInteger: As its name indicates, this data type is used for elements whose values must always be positive, starting at 1 up to ∞

nonPositiveInteger: This is for positive natural numbers starting at 0 up ∞

long: This long integer data type is used for very large natural numbers that range from -9223372036854775808 to 9223372036854775807. Remember that you can write the number is scientific format to make it easier to read

unsignedLong: This data type is used for long integers that must always be positive, ranging from 0 to 18446744073709551615

decimal: This type is for large floating-poiint numbers

float: This type is for floating-point numbers with single precision

double: This type is for floating-point numbers with double precision

date: This data type is reserved for date-based values. The value
can be set using the following format: YYYY-MM-DD:

YYYY represents 4 digits for the year

MM represents the digits for the month. If the value is less than 10, you can use or omit the leading 0

DD represents the digits for the day. If the value is less than 10, you can use or omit the leading 0

time: This data type is for time-based values. The value can
should be specified as 00:00:00.000 where:

The first two 0s represent the hours. If the hour value is less than 10, you use or omit the leading 0

The second two 0s represent the minutes. If the value is less than 10, you can use or omit the leading 0

The third combination of 0s represent the seconds. If the is less than 10, you can use or omit the leading 0

The three 0s represent the miliseconds

datetime: This type is for a combination of date and time using one of the following formulas:

YYYY-MM-DDT00:00:00.000 where YYYY, MM, and DD are the same as for the date data type. The T must for be used to separate the date and the time. 00:00:00.000 are the same as reviewed for the time data type

YYYY-MM-DDT00:00:00.000-00:00 where all values are as described above. The addition here is -00:00. This value is used to set the difference from the universal time

Notice that the video element has two child elements named title and director. When an XML element has a child element, that parent is said to have a complex type. In your XML schema, to create a tag for a parent element, create a tag named complexType and close it. This would be done as follows:

Between the start and end-tags of the complex type, you will create the tag(s) for the child element. But first, you must indicate that you are about to specify the order of the child elements. This is done by creating a tag named sequence. This can be done as follows:

Notice that the actors element, which is nested in the video element, has children elements of its own. For any node that has at least one child node, create a complexType start-tag and close it. Inside this complex type, create a sequence start-tag and close it. Inside the new sequence tag, create an element tag for each child element and specify its data type. Here is an example:

As you should know already, once an element has an attribute, it is said to have a complex type. Therefore, before creating a schema tag for the attribute, you must first create a complex parent type. Then create a child tag. Instead of the element keyword, use attribute. Here is an example:

In the same way, if a child element has an attribute, first create a complexType start-tag for it and close it. Inside this complex type, create a sequence start-tag and close it. Inside the new sequence tag, create an attribute tag for each attribute and specify its data type.

Options on Tags of an XML Schema

The Default Value of an Element or Attribute

By default, when creating an element or an attribute, you can give it any value you want. In fact, you create an element on one section of the document and give it a value, then create the same element in another section of the document but not give it a value. Here is an example:

If you have an element or an attribute that usually receives the same value, you can specify a default value for it. To do this, when creating the schema of the element or attribute, add a flag named default and assign the desired value to it. Here is an example:

Notice that the product element has two child nodes. When creating an XML document, you may want to require that the a certain element have at least a certain number of child elements. In that case, if the element has fewer numbers than the number of child elements, the document would be in violation of the rule and therefore would be invalid.

To specify that an element must have at least a certain number of child nodes, when creating its schema tag, add an attribute named minOccurs and assign the desired number to it. Here is an example:

This code indicates that the products element must have at least 4 child nodes. Since the products element in the above XML document has only two child nodes, the element violates the rule and would produce an error.

The Maximum Number of Occurrences of an Element

When creating an XML document, by default, you can create as many elements as you want and each element can have as many child nodes as necessary. If you want, you can limit the number of child nodes an element can have. To do this, when creating the schema tag of the element, add an attribute named maxOccurs and assign the desired value to it. The value can be either an insigned integer or unbounded. This can be done as follows:

In this case, the products element must not have more than 4 child nodes. In the XML document, if you create more child nodes for the element than the number specified in maxOccurs, the document would be violating the riule and would be considered invalid.

Since the minOccurs and the maxOccurs flags are not mutually exclusive, you can use them both on the same element. Here is an example:

Notice that the first gender element has a value of Male and the second has a value of Female. We could have created one more gender element and give it any value. In a typical XML document, especially in the absence of a schema, you can give any value to an element. In some cases, you can specify a constant value that must always be used for an element. To do this, when creating the schema tag of the element, add an attribute named fixed and assign the desired value to it. Here is an example:

Notice that the first product element has an attribute named number while the second product element does not have that attribute. When creating the schema of an element, you can ask that a certain attribute be required. To take care of this, when creating the schema of the attribute, add an attribute named use and assign a value named required to it. Here is an example:

Once this flag is set, whenever the element of that attribute is created, that attribute must be added. Otherwise, the element would violate the rule.

Setting an Attribute as Optional

Once again, consider the above XML document. Notice that the first product element has an attribute named number but the second product element does not have that attribute. When creating the schema of an element, you can add a certain attribute to a certain element but omit that attribute for the same element in another section of the document. In this cas, the attribute is said to be optional. To indicate this, when creating the schema of the attribute, add an attribute named use and assign a value named optional to it. Here is an example:

If you want to prevent the use of an element from a certain element in your XML document, create its tag in the XML schema document. Add both the minOccurs and the maxOccurs attributes and set the value of each to 0. Here is an example:

To prevent the use of a certain attribute, in the schema document, create its tag and add the use attribute. Assign a value as prohibited to it.

Setting a Constant Value to an Attribute

As seen for elements, the fixed flag can also be applied to an attribute, in which case you would require that an attribute always carry a certain value. In fact, you can add the use flag and specify whether the attribute must be required or it would be optional.