Saturday, January 2, 2010

Advantages of namespaces

Namespaces are a set of names, distinguished from other sets by being identified with a particular URI. It is a mechanism for differentiating elements. The syntax for defining a non-null prefix namespace is

xmlns:prefix:”URI”

This prefix followed by a colon is added to each tag within a vocabulary to make it unique. For example, if the prefix is ‘rdf’ then the elements would be of the form . As xmlns is a reserve word, we can establish all the namespace definitions in a given file by searching for xmlns.

They are indispensable part of XML documents so a clear appreciation of their role is a pre-requisite for a person embarking on XML/XSL development. The key benefits offered by namespaces are:

  • Facilitate use of different XML vocabularies in the same XML document by resolving conflicts stemming from identical tags being used in different vocabularies. The name clashes need to be averted. The namespace qualifier makes the tag globally unique, thus obviating any ambiguities. It is not unusual to have multiple vocabularies in a single document; for example XSLT stylesheet needs three different XML vocabularies so avoiding name clashes is of fundamental importance.
  • Provides a simple, abbreviated, XML-compliant prefix for a unique uniform resource identifier (URI), thus avoiding syntactical difficulties stemming from non-compliant characters if one were forced to use full URI for qualifying tags.
  • Improves readability - from a parser perspective short prefix is identical to full namespace name.
  • Allows an organisation to have their distinct tags with distinct meanings through different namespaces associated with different URIs and they can all be called within same XML document., ie Adobe can have different tags for different image formats and they can all be used in a single document,
  • Avoids tedium of typing by providing a mechanism for defining default namespace within a document so unqualified names automatically acquire this full qualification from parser perspective.
  • To constrain a document to an XML vocabulary, we need XML Schema which can only be defined by using the reserved namespace http://www.w3.org/2001/XMLSchema which has all the attributes and elements of W3C XML schema specification. Also the document validators check document instance against the structures, elements, attributes, datatypes and constraints defined in associated XML schema. The link to associated schema in a document instance is through a namespace. Incidentally, we should appreciate that a namespace URI does not necessarily point towards anything on the implied location.
  • Allows search engines to find similar documents with tags conforming to a namespace. In practice, namespace brings all the elements and attributes of a vocabulary together to be exploited by software.

No comments:

Post a Comment