This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, The purpose of XML namespaces is to distinguish between duplicate element type and attribute names. Such duplication might occur, for example, in an XSLT stylesheet or in a document that contains element types and attributes from two different DTDs. An XML namespace is a collection of element type and attribute names. The namespace is identified by a unique name, which is a URI. Thus, any element type or attribute name in an XML namespace can be uniquely identified by a two-part name: the name of its XML namespace and its local name. This two-part naming system is the only function of XML namespaces.
XML namespaces are declared with an xmlns attribute, which can associate a prefix with the namespace. The declaration is in scope for the element containing the attribute and all its descendants. For example: <!-- Declares two XML namespaces. Their scope is the A and B elements. --> <A xmlns:foo="http://www.foo.org/" xmlns="http://www.bar.org/"> <B>abcd</B> </A>
If an XML namespace declaration contains a prefix, you refer to element type and attribute names in that namespace with the prefix. For example: <!-- A and B are in the http://www.foo.org/ namespace, which is associated with the foo prefix. --> <foo:A xmlns:foo="http://www.foo.org/"> <foo:B>abcd</foo:B> </foo:A>
If an XML namespace declaration does not contain a prefix, the namespace is the default XML namespace and you refer to element type names in that namespace without a prefix. For example: <!-- This is equivalent to the previous example but uses a default namespace instead of the foo prefix. --> <A xmlns="http://www.foo.org/"> <B>abcd<B> </A>
Who can create an XML namespace? Anybody can create an XML namespace -- all you need to do is assign a URI as its name and decide what element type and attribute names are in it. The URI must be under your control and should not be being used to identify a different XML namespace, such as by a coworker. (In practice, most people that create XML namespaces also describe the element types and attributes whose names are in it -- their content models and types, their semantics, and so on. However, this is not part of the process of creating an XML namespace, nor does the XML namespace include or provide a way to discover such information.)