October 04, 2005

xhtml namespace

From webdesign-l mailing list (http://webdesign-l.com/mailman/listinfo/list ) list Digest, Vol 1, Issue 2, 10/3/2005

Gene Falck wrote:

>> FWIW, I have been faithfully typing the following
>> (without the extra space after the < and the extra
>> space before the > I inserted to prevent its being
>> interpreted as a tag):
>>
>> < html xmlns="http://www.w3.org/1999/xhtml"" xml:lang="en" lang="en" >

If your document is XHTML this is required - so well done for faithfully typing it, although I rather hope you actually use copy and paste to save the effort each time. ;)

If it is plain old HTML4 you don’t need it.

You shouldn’t ever claim a document as XHTML without using this namespace; in the (admittedly very unlikely unless you specifically make it happen) event that a browser actually treats it as XHTML/XML, you’ll require that namespace for it to work.

To give an example < a href="whatever" > in the XHTML namespace is a link. < a href="whatever" > without a namespace is no more meaningful than < froogleblarg sprong="whatever" > as far as the browser is concerned, and will/should be treated the same. The namespace is what really tells the browser how it should treat content.

(This is how you can have things like SVG inside XHTML files and the browser knows to treat it as SVG, rather than going ‘==< rectangle >?== but this is an xhtml file, there aren’t any rectangles in xhtml, I’ll ignore that’. The SVG tags are given a different namespace.)

If you want a more concrete demonstration, take the following source:

< html xmlns="http://www.w3.org/1999/xhtml">
< head>
< title>hey< /title>
< /head>
< body>
< p>hey< /p>
< /body>
< /html>

Save it as something.xhtml and try to view it in Firefox. You’ll see the following:

hey

And with ‘hey’ in the title bar. As expected, in short. Now, remove the namespace attribute and reload. You’ll see in the browser:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

? <html>
 ? <head>
    <title>hey</title>
   </head>
 ? <body>
    <p>hey</p>
   </body>
</html>

Probably not what was intended. :)

So, just to be clear - in practice it won’t make any difference as long as you serve your document as HTML and not as XHTML. But if you’re claiming that your document is XHTML then you probably should live up to that claim by making sure there’s some chance it actually works as XHTML. If you don’t want to do that, use the HTML 4.01 doctype instead, there’s no shame in that. [See my previous rant on the subject :)]

—sam

Posted by markp at October 4, 2005 11:56 AM
Comments
Post a comment









Remember personal info?





Enter this number to post your comment: