« Another Publisher Blog? Main | OMR Marks Again »

Empty Namespaces

In the Google Maps entry from a few daze back I mentioned that to use the XML coming from the Google API I needed to get funky in the template because of the XML format. I have had two inquiries this week about how to handle an empty namespace, so I thought I'd break out those paragraphs into an article.

the XML in question starts.

<kml xmlns="http://earth.google.com/kml/2.0">
 <Response>
  <name>500 Oracle Parkway</name>
  <Status>
   <code>200</code>
   <request>geocode</request> 

Looks innocuous enough but that first line will trip up the unwary RTF template developer. Its a namespace declaration, but you support namespaces right? They just need to be declared in the top of the template and then use the namespace prefix when referencing the elements in the XML. For instance, the following XML

<INVOICES inv="http://www.oracle.com/xml/2.0">
<INVOICE>
<NUMBER>1001021</NUMBER>

can be handled by declaring the namespace in the template thus:

<?namespace:inv="http://www.oracle.com/xml/2.0">

and then using 'inv' as a prefix to every data reference e.g.

<?inv:INVOICE?>

But this Google namespace has a problem - there is no prefix

<kml xmlns="http://earth.google.com/kml/2.0">

So how can you handle it? We just need to provide a dummy prefix in our template, for instance, in our template we declare..

<?namespace:x="http://earth.google.com/kml/2.0>

we then need to prefix all of our element names with 'x:'

<?x:coordinates?>

I know, you're asking what the heck do we need namespaces for in the first place? A kind Wikipedia soul has stated:

'In general, a namespace is an abstract container providing context for the items (names, or technical terms, or words) it holds and allowing disambiguation of items having the same name (residing in different namespaces).'

'Disambiguation'? is that a word? Basically, namespaces allow you to uniquely identify a set of names so that there is no ambiguity when objects having different origins but the same names are mixed together.

For example:

<INVOICES >
 <INVOICE>
  <NUMBER>1001021</NUMBER>
  <NUMBER>1001011</NUMBER>

you can not discern between the INVOICE values however with the use of a namespace

<INVOICES inv="http://www.oracle.com/xml/2.0">
 <INVOICE>
  <NUMBER>1001021</NUMBER>
  <inv:NUMBER>1001011</NUMBER>

we can now reference the two NUMBER elements, they are distinct from each other.

So, why do Google have an empty namespace? This might be down to the developer at Google that wrote the code neglected to specify a prefix. Maybe it was done to keep me awake when I blogged it either way, we can and now you can handle it :0)

Post a comment