XML namespace fun

TorgoX on 2005-10-19T07:32:58

Dear Log,

So, if you're dealing with XML namespaces, like so:

 
   
     
       
     
     
     
     
   
 

...then you can just make one of the namespaces the default so that you can omit the prefix for it, like so:

 
   
     
       
     
     
     
     
   
 

or:

 
   
     
       
     
     
     
     
   
 

... where presumably you will have the default namespace be whichever one is most common in your document.

Of course, this is well-understood among all advanced superbeings of pure XML energy.

But what is less appreciated is that, for the nondefault namespaces, it may be a good idea to use a short and/or easy to type namespace prefix.

In other words, consider that this:

 
   
     
       
     
     
     
     
   
 

is the same as this, but just more concise:

 
   
     
       
     
     
     
     
   
 

The only difference is that the latter uses "Q" as the namespace prefix instead of "xsl". For the parse, it's the namespace name ("http://www.w3.org/1999/XSL/Transform") that matters, not the prefix ("xsl"/"Q").

I use "Q" because it's right next to ":" on my keyboard. It could just as easily be "_" or "x" or "é" or "φ" or "乒" or whatever "letter" you like. (Although clearly this could be abused, just like with short variable names in programs.)

Here ends your soul-shattering revelation for the day.


Hmmm....

Dom2 on 2005-10-19T16:11:37

Whilst there's nothing technically wrong with doing that, it will certainly cause people who read it to do a double take. As an example, imagine if you're reading some mod_perl code and the author has decided to use $QUERY instead of the usual $r. It's legitimate to do so, but certainly not idiomatic.

Of course, if you're the only one who's going to see it, do as you will!

-Dom