Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

Namespaces

The problem outlined in the previous example is the extensible character of XML. There is no way to prevent somebody from extending a document in a way that is incompatible with other works. That's the nature of extensibility. Because anybody can create tags, there is a huge risk of conflicts.

One solution to prevent conflicts would be to establish a global registry of accepted tags and their associated definition. It would however severally limit XML’s flexibility.

Nobody wants to limit XML's flexibility. Flexibility was a major goal in the design of XML. The namespaces proposal addresses this problem with a more elegant approach: it does not limit extensibility but it introduces mechanisms to manage it.

Listing 6 is equivalent to Listing 4 but it uses namespaces to prevent naming clashes.

Listing 6 Using namespaces.

<?xml version="1.0"?>
<references xmlns:qa="http://joker.playfield.com/star-rating/1.0"
            xmlns:pa="http://penguin.xmli.com/review/1.0"
            xmlns="">http://catwoman.pineapplesoft.com/ref/1.5">
   <name>Macmillan</name>
   <link href="">http://www.mcp.com"/>
   <qa:rating>5 stars</qa:rating>
   <pa:rating>G</pa:rating>
   <name>Pineapplesoft Link</name>
   <link href="">http://www.pineapplesoft.com/newsletter"/>
   <qa:rating>5 stars</qa:rating>
   <pa:rating>G</pa:rating>
   <name>XML.com</name>
   <link href="">http://www.xml.com"/>
   <qa:rating>4 stars</qa:rating>
   <pa:rating>G</pa:rating>
   <name>Comics.com</name>
   <link href="">http://www.comics.com"/>
   <qa:rating>5 stars</qa:rating>
   <pa:rating>G</pa:rating>
   <name>Fatbrain.com</name>
   <link href="">http://www.fatbrain.com"/>
   <qa:rating>4 stars</qa:rating>
   <pa:rating>G</pa:rating>
   <name>ABC News</name>
   <link href="">http://www.abcnews.com"/>
   <qa:rating>3 stars</qa:rating>
   <pa:rating>PG</pa:rating>
</references>

At first sight, Listing 6 is similar to Listing 5: It declares two different names for the ratings.

The major different is the form of the names. In Listing 6 a prefix is added before each element name. A colon separates the name and the prefix:

<qa:rating>5 stars</qa:rating>

The prefix unambiguously identifies the type of rating in this document. However prefixes alone solve nothing because anybody can create prefixes. Therefore, different people can create incompatible prefixes and we are back to step one: we have move the risk of conflicts from element names to prefixes. To avoid conflicts in prefixes, the prefixes must be declared:

<references xmlns:qa="http://joker.playfield.com/star-rating/1.0"
            xmlns:pa="http://penguin.xmli.com/review/1.0"
            xmlns="">http://catwoman.pineapplesoft.com/ref/1.5">

The declaration associates a URI (Uniform Resource Identifier --a superset of the popular URLs) with a prefix. This is the crux of the namespaces proposal because URIs, unlike names, are unique. Namespaces piggyback on the registration mechanisms established for URIs.

For example, URLs are guaranteed to be unique because they are based on domain names. Domain names are registered to guarantee uniqueness.

Namespace declaration is done through attributes with the prefix xmlns followed by the prefix. In Listing 6, two prefixes are declared: qa and pa.

The attribute xmlns declares the default namespace, that is the namespace for those elements that have no attributes.

Warning! Namespaces use URLs as identifiers only. The URLs need not point to a web site. This is often confusing to XML newcomers: after all, why use URLs if they don’t point to a web site? To benefit from the domain name registration. There’s no other reasons.

In other words, if you type an URL taken from a namespace in your web browser and you get the error “404 Page Not Found”, it’s normal.

In summary, XML namespaces is a mechanism to unambiguously identify who has developed which element. It's not much but it is an essential service.

  • + Share This
  • 🔖 Save To Your Account