Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

TorgoX (1933)

TorgoX
  sburkeNO@SPAMcpan.org
http://search.cpan.org/~sburke/

"Il est beau comme la retractilité des serres des oiseaux rapaces [...] et surtout, comme la rencontre fortuite sur une table de dissection d'une machine à coudre et d'un parapluie !" -- Lautréamont

Journal of TorgoX (1933)

Wednesday October 19, 2005
02:32 AM

XML namespace fun

[ #27240 ]
Dear Log,

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

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:html="http://www.w3.org/TR/xhtml1/strict"
>
   <html:shunk>
     <xsl:wuggawugga>
       <html:stuff />
     </xsl:wuggawugga>
     <xsl:mukluk />
     <html:wugga />
     <xsl:mukluk />
   </html:shunk>
</xsl:stylesheet>

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

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns    ="http://www.w3.org/TR/xhtml1/strict"
>
   <shunk>
     <xsl:wuggawugga>
       <stuff />
     </xsl:wuggawugga>
     <xsl:mukluk />
     <wugga />
     <xsl:mukluk />
   </shunk>
</xsl:stylesheet>

or:

<stylesheet version="1.0"
   xmlns     ="http://www.w3.org/1999/XSL/Transform"
   xmlns:html="http://www.w3.org/TR/xhtml1/strict"
>
   <html:shunk>
     <wuggawugga>
       <html:stuff />
     </wuggawugga>
     <mukluk />
     <html:wugga />
     <mukluk />
   </html:shunk>
</stylesheet>

... 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:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns    ="http://www.w3.org/TR/xhtml1/strict"
>
   <shunk>
     <xsl:wuggawugga>
       <stuff />
     </xsl:wuggawugga>
     <xsl:mukluk />
     <wugga />
     <xsl:mukluk />
   </shunk>
</xsl:stylesheet>

is the same as this, but just more concise:

<Q:stylesheet version="1.0"
   xmlns:Q="http://www.w3.org/1999/XSL/Transform"
   xmlns  ="http://www.w3.org/TR/xhtml1/strict"
>
   <shunk>
     <Q:wuggawugga>
       <stuff />
     </Q:wuggawugga>
     <Q:mukluk />
     <wugga />
     <Q:mukluk />
   </shunk>
</Q:stylesheet>

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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