XML Namespaces
by Dinesh[ Edit ] 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â€?>
<rootelement>
<firstelement
xmlns:fe=â€http://www.benztech.com/schemas/verybasicâ€
position=â€1â€>
<fe:level1 children=â€0â€>This is level 1 of the nested
elements</fe:level1>
</firstelement>
<secondelement
xmlns:se=â€http://www.benztech.com/schemas/verybasicâ€
position=â€2â€>
<se:level1 children=â€1â€>
<se:level2>This is level 2 of the nested
elements</se:level2>
</se:level1>
</secondelement>
</rootelement>
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.