XML Namespaces

To avoid element name conflicts, a method is facilitated by XML Namespaces.

Name Conflicts:

Defined by the developer, the element names in XML certainly results in a conflict while mixing XML documents from different XML applications.

Example 1:

Name Department

Explanation:

In the above example, the XML carries the HTML table information.

Example 2:

Side Table2622

Explanation:

In the above example, the XML carries the information about a table i.e., a piece of furniture.

There would surely be a name conflict if the above two XML fragments were added together. The reason is simple, the <table> element is present in both the fragments, but with different content and meaning. To handle these differences, the user or an XML application may no be capable.

Solving the Name Conflict Using a Prefix:

A name prefix is used to avoid name conflicts in XML.

Example:


  
    Name
    Department
  



 Side Table
  26
  22

Explanation:

In the above example, the XML carries information about an HTML table and a piece of furniture, but with no conflict because the two <table> elements have different names.

XML Namespaces – The xmlns Attribute:

A namespace for the prefix must be defined while using prefixes in XML. The xmlns attribute is used in the start tag of an element, to define a namespace. Defining a namespace for an element means that all the child elements with the same prefix are associated with the same namespace.

Syntax: Namespace declaration:

xmlns:prefix="URI"

Example:



  
    Name
    Department
  



 Side Table
  26
  22


Explanation:

In the above example, the first <table> element xmlns attribute provides a qualified namespace to the h: prefix, while the second <table> element xmlns attribute provides a qualified namespace to the f: prefix.

Example: Declaring Namespaces in the XML root element:




  
    Name
    Department
  



 Side Table
  26
  22



Uniform Resource Identifier (URI):

A string of characters that is used to identify an Internet Resource, is called a Uniform Resource Identifier (URI). The Uniform Resource Locator (URL) is the most common URI. It identifies an Internet domain address. The Uniform Resource Name (URN) is a not so common type of URI.

To check the information, the namespace URI is not used by the parser and the main purpose of using a URI is to give the namespace a unique name. The namespace is, however, often used as a pointer to a web page containing namespace information.

Default Namespaces:

To save us from using prefixes in all the child elements, we can define a default namespace for an element.

Syntax:

xmlns="namespaceURI"

Example 1:

Name Department

Explanation:

In the above example, the XML carries the HTML table information.

Example 2:

Side Table2622

Explanation:

In the above example, the XML carries the information about a table i.e., a piece of furniture.

Namespaces in Real Use:

To transform XML documents into other formats, the XSLT language is used.

Example:








  

My Music Collection

Song Artist

Explanation:

In the above example, the XML document is used to transform XML into HTML. To identify the XSLT elements inside an HTML document, the namespace “http://www.w3.org/1999/XSL/Transform” is used.

Please follow and like us:
Content Protection by DMCA.com