Home Blog Tech Stuff Serving XHTML Content With Joomla!
Serving XHTML Content With Joomla! PDF Print E-mail
Saturday, 26 April 2008 16:52

XHTML has been around long enough now that most people have heard of it, and most web developers know what to do to ensure their code produces valid XHTML. You will often see little notes in page footers claiming "Valid XHTML" or similar, and often enough, it even is valid XHTML. Unfortunately, however, the overwhelming majority of valid XHTML web pages might as well not be, because the browser has no idea that they are, and treats them as the bog-standard tag soup anyway.

The reason? The MIME type specified in the HTTP Content-Type header claims that the page is "text/html" or similar, which is what the browser pays attention to, not the DOCTYPE element.

The culprit? Microsoft (obviously). Internet Explorer (even the new version 8) can't quite handle XHTML properly and if you try serving it XHTML documents with the correct MIME type (application/xhtml+xml), it will simply offer to download the page for you.

But if you're sick of waiting for Microsoft to play the game (that being either co-operation or catch-up, depending on your political views) and you want to start using XHTML-based technologies (stuff like XForms, SVG, RDFa and MathML), serving XHTML for what it actually is works just fine on "all Mozilla-based browsers, such as Mozilla, Netscape 5 and higher, Galeon and Firefox, as well as Opera, Amaya, Camino, Chimera, DocZilla, iCab, Safari, and all browsers on mobile phones that accept WAP2. In fact, any modern browser." —W3C FAQ

And if you just want to serve up the correct MIME type only to browsers that understand it, there's always the HTTP_ACCEPT header. Basically, you can just check this header that is sent by the browser, to see what content types it supports, then set the Content-Type header appropriately in code. Of course, this doesn't help much if you're actually trying to use XHTML for something - it still won't work in IE, but not everybody has to care; for example, in situations where you can control the browser environment (as with some corporate intranets) or if your primary audience is Linux users, you might be happy to inform IE users that they need a modern, standards-compliant browser, or point them towards the various IE plugins and hacks around that can be used to provide some of the missing support (I might compile a directory of these when I have more time).

Anyway, since I'm going to be building stuff to add semantic markup to Joomla! content, one of the main prerequisites is XHTML. And as standard, Joomla! won't serve pages as XHTML, unless you've configured your web server to do it, which is probably not a good idea - for starters, the Joomla! Administrator (and certain parts of the front-end that use forms) won't work as XHTML. One of the main reasons for this is that Joomla! uses an old DOM Level 0 means of accessing the element named adminForm (document.adminForm) in pretty much all of it's Javascript, which won't work with the XML DOM you get with genuine XHTML documents. There are lots of other places where XHTML is broken in Joomla! (in a few cases it doesn't even output well-formed XML) but most of these are in the administration back-end. It's relatively easy to get the front-end working fully as XHTML if you're not doing much more than publishing content using the core components and I'll post more details on this when have time. But for now, I just wanted an easy way to switch XHTML on or off, for development purposes. So I've built a simple little plugin for Joomla! that lets you do just that.

Once installed and enabled, all it will do is check for browser support via HTTP_ACCEPT, and change the Content-Type header where possible. It doesn't (yet) change the meta tag for the content type, or anything else that might be useful or necessary, but it's a place to start, and I'm making it available here in the hope that somebody either finds it useful for what it already is, or improves it for me first :)

Comments
RSS
Thomas     |2008-06-12 12:20:38
Hi, really cool.

This is exactly what I was looking for.
John Velling  - how did you get it to work?     |2008-07-22 16:01:55
I have tried this extension in order to use MathML, and have not figured
out how to display a basic example. Look at Chapter 1, First Trial
at the URL

http://www.lastriteseggnog.com/MathTest/index.p...

If I enable the plug-in, I cannot even open the chapter. If I disable, I
get

x2

I have tried this with brackets - [] - rather than angles -
- as delimiters, but this does not seem to work.

JAV
Will  - Updated Version   |2008-06-25 19:57:43
I've made an updated version of this plugin that works through the
onAfterDispatch event and sets the headers using JDocument methods
setMimeEncoding and setCharset, instead of directly using JResponse::setHeader
in onAfterRender. One advantage I discovered for this method is that JDocument's
addScriptDeclaration can check the MIME type and properly escape the javascript
for XHTML.
Thomas  - re: Updated Version     |2008-07-23 07:48:52
Thanks, I've reinstalled my Joomla with the newest version of Joomla (1.5.4) and
installed the updated version of your plugin and it works without any problems
now.
John Velling  - Need documentation     |2008-07-22 15:54:20
I spent most of last night trying to figure out how to make this plug-in
work. Not so obvious, at least to me. When I enable the plug-in, I get
a basic parsing error and cannot even get in to "Chapter 1".
When disabled, the article "First Trial" shows up as
the text

x2

Here it is disabled... http://http://x2

Easy to fix? You probably need an example or two posted.

JAV
Will   |2008-07-27 20:46:23
John, if you get a parsing error then something in Joomla! is generating
mal-formed XML. Even just in the Joomla! core there are a few places where this
happens though the main culprits in my experience are 3rd party extensions. This
is precisely why people should try to serve proper XHTML so that they catch
these bugs as soon as they write them.

Perhaps I should have made clearer that
this is not something for the average user to play with - you will need to be
able to modify a few bits of code to work around the various issues with your
site not producing valid XHTML in places. There's not much point in me starting
to write up examples/tutorials in relation to this until I've finished the work
I'm doing right now to fix all the XHTML issues in the Joomla! core. I tried
posting in the forums to get the Joomla! core developers involved, but nobody
seems interested, so I'm not sure yet what form a more complete solution will
Gareth   |2008-08-20 01:59:00
Are you using your plugin for this website? It appears to be serving text/html?
Will   |2008-08-23 18:33:11
No, I'm not currently using it here. I use it only for my development stuff.
Most Joomla! extensions (such as JoomlaComment used here) and some of the core,
do not output well-formed XML, let alone valid XHTML. That's one reason for
using this plugin - to catch such errors as you create them during development.
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Attachments:
 plgSystemXHTML-0.2.zip[An updated version that sets the MIME type and charset through JDocument methods in the onAfterDispatch event.]2 Kb
 plgSystemXHTML.zip[Joomla! System Plugin For XHTML]2 Kb
Last Updated on Friday, 27 June 2008 05:24
 
 

Sponsorship

Visitors are kindly requested to give the advertisements at least a cursory glance.