SCDJWS Study Guide: XML Namespace
An XML namespace is a collection of names, identified by a URI reference [RFC2396], which are used in XML documents as element types and attribute names. XML namespaces differ from the "namespaces" conventionally used in computing disciplines in that the XML version has internal structure and is not, mathematically speaking, a set.
URI references which identify namespaces are considered identical when they are exactly the same character-for-character. Note that URI references which are not identical in this sense may in fact be functionally equivalent. Examples include URI references which differ only in case, or which are in external entities which have different effective base URIs.
Names from XML namespaces may appear as qualified name, which contain a single colon, separating the name into a namespace prefix and a local part. The prefix, which is mapped to a URI reference, selects a namespace. The combination of the universally managedURI namespace and the document's own namespace produces identifiers that are universally unique. Mechanisms are provided for prefix scoping and defaulting.
URI references can contain characters not allowed in names, so cannot be used directly as namespace prefixes. Therefore, the namespace prefix serves as a proxy for aURI reference. An attribute-based syntax described below is used to declare the association of the namespace prefix with aURI reference; software which supports this namespace proposal must recognize and act on these declarations and prefixes.
Namespaces in XML
Any element or attribute in an XML namespace is uniquely identified by two parts: a namespace name (typeically aURI) and a local name. Such a two-part name is known as a qualified name or QName.
In an XML document we use a namespace prefix to qualify the local names of both elements and attributes. A prefix is really just an abbreviation for the namespace identifier (URI) which is typically quite long and is a painful job to add it to element and attribute names in order to make them unique.
The prefix is first mapped to a namespace identifier through a namespace declaration. The syntax for a namespace declaration is:
A namespace declaration looks just like an attribute (on an element), but they're not officially considered attributes in terms of the logical document structure (that is, they won't appear in an element's attributes collection when using theDOM).
An example namespace declaration, which associates the namespace prefix soap with the namespace name http://schemas.xmlsoap.org/soap/envelope:
<!-- the "soap" prefix is bound to http://schemas.xmlsoap.org/soap/envelope -->
<!-- for the "Envelope" element and contents -->
Although a namespace usually looks like a URL, that doesn't mean that one must be connected to the Internet to actually declare and use namespaces. Rather, the namespace is intended to serve as a virtual "container" for vocabulary and undisplayed content that can be shared in the Internet space. In the Internet space URLs are unique�hence you would usually choose to use URLs to uniquely identify namespaces. Typing the namespace URL in a browser doesn't mean it would show all the elements and attributes in that namespace; it's just a concept.
Note that the address used to identify the namespace, is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to real Web page containing information about the namespace.
The namespace attribute is placed in the start tag of an element. A namespace prefix is considered in-scope on the declaration element as well as on any of its descendant elements. Once declared, the prefix can be used in front of any element or attribute name separated by a colon (such as s:student). This complete name including the prefix is the lexical form of a qualified name (QName):
The prefix associates the element or attribute with the namespace identifier mapped to the prefix currently in scope.
The following XML document describes about a room element. With two different namespaces defined for prefix 'h' and 'f', the 'table' elements are not conflict inside one XML document.
<?xml version="1.0" encoding="UTF-8" ?>
<f:name>African Coffee Table</f:name>
Since all XML documents must be well-formed, the start-tag to every element must have a matching end-tag. If a namespace declaration is the attribute to an element, it must go in an element start-tag. A namespace declaration is "in scope" for the entire content of that element, including the other attributes in the start-tag itself.
Since namespaces "h" and "f" were declared in the document element of the room document, any element or attribute in the document with the prefix "h:" is identified with the given http://www.w3.org/TR/html4 URI.
It is not possible to get ride of the prefix and qualify an XML element directly with the namespace name identifier. Let's rewrite the above XML document:
QName = <prefix>:<local name>
<?xml version="1.0" encoding="UTF-8" ?>
<http://www.w3schools.com/furniture:name>African Coffee Table</http://www.w3schools.com/furniture:name>
Such an XML document is unreadable regardless of the fact that the syntax is not valid, as there are two colons (":") in the qualifier.