XML Namespaces

by Dinesh 2012-07-22 22:54:24

<h3>XML Namespaces</h3>Namespaces are a method for separating and identifying duplicate XML element names in an XML document. Namespaces can also be used as identifiers to describe data types and other information. Namespace declarations can be compared to defining a short variable name for a long variable (such as pi=3.14159....) in programming languages. In XML, the variable assignment is defined by an attribute declaration. The variable name is the attribute name, and the variable value is the attribute value. In order to identify namespace declarations versus other types of attribute declarations, a reserved xmlns: prefix is used when declaring a namespace name and value. The attribute name after the xmlns: prefix identifies the name for the defined namespace. The value of the attribute provides the unique identifier for the namespace. Once the namespace is declared, the namespace name can be used as a prefix in element names.
<?xml version=”1.0” encoding=”UTF-8”?>
<fe:level1 children=”0”>This is level 1 of the nested
<se:level1 children=”1”>
<se:level2>This is level 2 of the nested
In this example, I am using two namespaces as identifiers to differentiate two level1 elements in the same document. The xmlns: attribute declares the namespace for an XML document or a portion of an XML document. The attribute can be placed in the root element of the document, or in any other nested element.

The namespace name for the firstelement element is fe, and the namespace name for the secondelement is se. Both use the same URL as the value for the namespace. Often the URL in the namespace resolves to a Web page that

provides documentation about the namespace, such as information about the data encoding types identified in the namespace. However, in this case, we are just using the namespace to defined prefixed for unique identification of duplicate element names. The URL does resolve to an actual document, but is just used as a placeholder for the namespace name declarations.

You must LOGIN to add comments