XML Namespace

Namespace

To avoid the conflict in xml schema namespace is the solution.

It provides the way to define a unique identifier in a schema context. In an XML schema, you can create elements and types, which are named distinctively within the namespace of the schema. An element is the namespace’s definition scope, which can override a namespace declaration defined in a parent element. Given that a namespace defines a context in which identifiers must be unique, schemas can safely integrate definitions from other namespaces—even those with the same names.

We will show it here with example,


This XML carries HTML table information:

  1. <table>
  2. <tr>
  3. <td>Oranges</td>
  4. <td>Mangoes</td>
  5. </tr>
  6. </table>

This XML carries information about a table (a piece of furniture):

  1. <table>
  2. <address>Home Address</ address >
  3. <phone>802323234</phone>
  4. </table>

If above xml schemas added together, there would be a conflict. Both of the above contain same element name, <table>, even though the purpose and inner elements are different.

An XML parser should not know that both are different.

To solve this conflict we are going to use XML Namespace.

  1.  
  2. <root xmlns:h=<a href="http://www.w3.org/TR/html4/">http://www.w3.org/TR/html4/</a> xmlns:f="http://www.w3schools.com/furniture">
  3.  
  4. <h:table>
  5. <h:tr>
  6. <h:td>Oranges</h:td>
  7. <h:td>Mangoes</h:td>
  8. </h:tr>
  9. </h:table>
  10.  
  11. <f:table>
  12. <f:address>Home Address</f:address >
  13. <f:phone>802323234</f:phone>
  14. </f:table>
  15.  
  16. </root>

XML Namespace

With the xmlns attribute in an XML schema, you can define the following:

  • A default namespace, which applies to elements with no prefixes.
  • Prefixes that reference other namespaces. A prefix is an abbreviation for a namespace. To define a prefix, append a colon (:) and the prefix name to xmlns.

A prefix is associated with URI

The association is defined as an attribute within an element

xmlns:prefix

xmlns is Namespace keyword, prefix is user-defined

For example,

  1. <classes  xmlns:XMLclass=“http://www.brandeis.edu/rseg-0151-g”>
  2.  
  3. <XMLclass:syllabus>
  4.  
  5.  
  6. </XMLclass:syllabus>
  7.  
  8. </classes>
  • Can be declared in a root element or at lower level element
  • Multiple different namespaces can be defined
  • Same prefix can be redefined within a same document
  • Scope of Namespace declaration is within the element where it is defined

Namespace URI

A Uniform Resource Identifier (URI) is a string of alpha numeric characters which identifies an Internet Resource.

The mainly widespread URI is the Uniform Resource Locator (URL) which uniquely identifies an Internet domain address. One more, not so familiar form of URI is the Universal Resource Name (URN).

URI cannot be prefix

  • “/”, “%”, and “~” are not legal in XML element names
  • URI could be standardized (by industry standard orgs) while prefixes are just convention
  • URI are just “identifiers”
  • URI does not have to be in “http” form
  • URI does not have to be resolved
  • It is like a “constant value”
  1.  
  2. xmlns="<em>namespaceURI</em>"
  3.  
  4. <root xmlns:XMLclass=“http://www.techstop.abidshafiq.com/rseg-0151-g”>
  5.  
  6. <XMLclass:syllabus>
  7.  
  8.  
  9. </XMLclass:syllabus>
  10.  
  11. </root>

Default Namespaces

Defining a default namespace for an element reduces the cost of using prefix in all child elements.

  • Declared with xmlns attribute with no prefix
  • Applied only to unprefixed element and its descendant elements
  • Applies only to elements not attributes
  1.  
  2. <table xmlns="http://www.w3.org/TR/html4/">
  3. <tr>
  4. <td>Oranges</td>
  5. <td>Mangoes</td>
  6. </tr>
  7. </table>
Related Links: You can leave a response, or trackback from your own site.

Add a Facebook Comment

14 Responses to “XML Namespace”

  1. Issac Maez says:

    I was just having a conversation over this yesterday I am glad I came across this it cleared some of the questions I had. I agree with the previous post about that I will be checking back to this blog.

  2. Dennis says:

    Finally, a good post about this subject! I can not believe I had to go thru like 20 blogs just to come across this excellent post. All their content together still can not equal the content of this article. I have learned a lot and will bookmark your web page. Maintain up the good job!

  3. Sylvester P. says:

    I enjoy your website. it seems to be so excellent for me as well as I am sure to everyone the visitors here!

  4. Cialis says:

    2kKWRK Excellent article, I will take note. Many thanks for the story!

  5. Lestai says:

    Yhanks for your great website! ^^

    Lesbian Hentai

  6. Found this blog on Twitter, and I just needed to say thanks for the information!

  7. Arron Persia says:

    This is a usefull post.

  8. Primarily based on your expertise of posting this blog which is easier between blogengine and wordpress blogs?

  9. gaucho says:

    Interesting element to subject material content. I just stumbled on your web site and in accession richesse to assert that I get in reality cherished account your weblog posts. Any way I will be subscribing for your feeds or even I fulfillment you accessibility routinely fast.

  10. Howdy! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My blog addresses a lot of the same topics as yours and I feel we could greatly benefit from each other. If you are interested feel free to shoot me an email. I look forward to hearing from you! Great blog by the way!

  11. Tono Aguan says:

    Very interesting subject , regards for putting up. “Nobody outside of a baby carriage or a judge’s chamber believes in an unprejudiced point of view.” by Lillian Hellman.

  12. Roy Bomar says:

    Good day there admin, I clearly wished to actually place a quick statement to say that I loved your specific information. Thanks!

Leave a Reply