XML Namespace



XML Namespace

  • In XML, a namespace is used to prevent any conflicts with element or attribute names.
    • XML allows you to create your own element names, there's always the possibility of naming an element exactly the same as one in another XML document.
    • It is OK if you never use both documents together.
    • You would have a name conflict. But you want to combine the content of the both documents
    • We would have two different elements, with different purposes, both with the same name. In this case we use Namespace for the element to avoid the confusion.
 XML Namespace

Learn XML - XML tutorial - XML Namespace - XML examples - XML programs

  • Namespaces allows the browser to :
    • Combine various sources of documents.
    • It helps to identify the source of elements or attributes.
    • The Uniform Resource Locator (URL) contains the reference for a document or an HTML page on the Web.
 XML Namespace

Learn XML - XML tutorial - XML Namespace - XML examples - XML programs

Example Name Conflict

  • Imagine we have an XML document containing a list of books. Something like this:
 Example Name Conflict

Learn XML - XML tutorial - Name Conflict - XML examples - XML programs

  • And imagine we want to combine it with the following HTML page.
 Combination of XML Document

Learn XML - XML tutorial - Combination of XML Document - XML examples - XML programs

  • We will encounter a problem if we try to combine the above documents. This is because they both have an element called title. One is the title of the book, the other is the title of the HTML page. We have a name conflict.
  • When we prevent this name conflict we want to create a namespace for the XML document.

Example for Namespace

Example For Namespace

Learn XML - XML tutorial - Example For Namespace - XML examples - XML programs

 Example For Namespace

Learn XML - XML tutorial - Example For Namespace - XML examples - XML programs

  • We have added the xmlns :{prefix} attribute to the root element. We have assigned this attribute a unique value. This unique value is usually in the form of a Uniform Resource Identifier (URI).The URI defines the namespace .
  • And, now that the namespace has been defined, we have added a bk prefix to our element names.
  • Now, when we combine the two documents, the XML processor will see two different element names: bk:title (from the XML document) and title (from the HTML document).
 Example for Namespace Combination

Learn XML - XML tutorial - Example for Namespace Combination - XML examples - XML programs

Local Namespace

  • If we want two combine two document simply created a namespace to avoid a name conflict between the elements of two documents.
  • When we defined the namespace, we defined it against the root element.
  • This meant that the namespace was to be used for the whole document, and we prefixed all child elements with the same namespace.
  • You can also define namespaces against a child node.
 Local Namespace

Learn XML - XML tutorial - Local Namespace - XML examples - XML programs

Example for Local Namespace

 Local Namespace

Learn XML - XML tutorial - Local Namespace - XML examples - XML programs

Multiple Namespace

  • On the Same way of local namespace, you could use multiple namespaces within the same document if required.
  • That means the documents contains more than one similar elements then we use multiple namespaces.
  • For Example a document contains looping of author name we use a namespace for the root author and another namespace for the child one.
Multiple Namespace

Learn XML - XML tutorial - Multiple Namespace - XML examples - XML programs

Example for Multiple Namespace

 Multiple Namespace

Learn XML - XML tutorial - Multiple Namespace - XML examples - XML programs

Default Namespace

  • Default namespace define without the prefix.
  • Then all descendant elements below the namespace element are assumed to belong to that namespace unless specified the prefix.
 Default Namespace

Learn XML - XML tutorial - Default Namespace - XML examples - XML programs

Example for Default Namespace

 Default Namespace

Learn XML - XML tutorial - Default Namespace - XML examples - XML programs



Related Searches to XML Namespace