adahas

අදහස්

Download Day

application/xhtml+xml or text/html?

As you may already know, the MIME content-type that is generally used to serve HTML pages is text/html. But as we move ahead, problems with HTML are being identified and the new XHTML was born.

The specification for XHTML 1.0 by W3C introduces it as,

This specification defines the Second Edition of XHTML 1.0, a reformulation of HTML 4 as an XML 1.0 application, and three DTDs corresponding to the ones defined by HTML 4. The semantics of the elements and their attributes are defined in the W3C Recommendation for HTML 4. These semantics provide the foundation for future extensibility of XHTML. Compatibility with existing HTML user agents is possible by following a small set of guidelines.

Simply speaking, XHTML is a combination of HTML and XML that has been introduced for standardizing web content. If you would like to know more about XHTML you can see the XHTML 1.0 Specification, XHTML 1.1 Specification and the XHTML 2.0 Specification Draft.

In these specifications of XHTML 1.1, which is the latest, W3C recommends that the content should be served as application/xhtml+xml. But the problem of doing so is that some browsers (such as IE, obviously) doesn’t support the content-type yet. So what’s the decision? Go as plain text/html or go for application/xhtml+xml regardless of the browser compatibility problem. It’s easy to go for the first, but then it’ll not be conforming to the specification. The second may be your choice if you are a ‘standards person’ but then, what will happen to the people trying to view your site from a incompatible browser?

The answer is to go for the both. Both? How can a page be served as text/html and application/xhtml+xml both at the same time? No, actually a page cannot be served as two types at once, but it can be served as appropriate. Confused?

Using a scripting language, it can be checked whether the browser supports the content-type which it is going to be served as, before it is actually served. And if it doesn’t there’s the option to switch to the alternative (a fall-back option).

Let’s see how it can be done using PHP. You don’t need to know much of PHP to do this, in-fact you need not know anything, you can just copy and paste the code at the bottom of this page on your page and that’s it.

So, for the ones who like to know what’s really going on, here’s the explanation. Firstly, we’ll check whether the browser accepts application/xhtml+xml. The following code segment can be used to check whether the content-type is supported.


if(strstr($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml')) {
  // what to do if supported
}
else {
  // what to do if not supported
}

Then, if the type is supported (or not), content-type can be specified to the browser by using the following statement.


header(‘Content-type: application/xhtml+xml; charset=UTF8’);

But, it’s important that this should be done before any output is sent to the browser, not even a space. So this means the document that contains the above code should start with a PHP block, not even a space before the ‘<?php’, otherwise it’ll give a warning saying that ‘Cannot modify header information - headers already sent …’

So that’s it. If you didn’t understand anything up to now or don’t wanted to understand, but only want to serve the content as application/xhtml+xml, just copy and paste the following code segment at the top of your page and make the file a PHP file (rename from .html to .php). But remember, nothing should be there before the ‘<?php’, not even a space.


<?php
if(strstr($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml')) {
  header(‘Content-type: application/xhtml+xml; charset=UTF8’);
}
else {
  header(‘Content-type: text/html; charset=UTF8’);
}
?>

If you use the above code on your site, your pages will be served as application/xhtml+xml to browsers which supports it and for others as text/html.

Have fun!

4 responses to 'application/xhtml+xml or text/html?'

  1. Jim Dabell says:

    Simple string matching with strstr is not suitable if you want to write compliant code. Section 3.9 of RFC 2616 states that if a quality value of 0 is given during content negotiation, it means that content with that media type is not acceptable by the client. Thus, if a client explicitly states that application/xhtml+xml is not acceptable, you will serve it to them in preference to text/html, which is incorrect behaviour.

    The correct approach is to split the string up on commas then each result on semi-colons. You can then pull out the quality values for each media type.

    Another benefit to doing it this way is that you will serve text/html to clients that can handle application/xhtml+xml, but work better with text/html (previous versions of Firefox and Mozilla, for instance).

    You also aren’t sending a Vary response header to indicate that you are varying content in relation to the Accept header, which can play havoc with caches. See RFC 2616, section 14.44.

  2. Jim: Firstly, thank you for contribution, and sorry for the delay in replying.

    Totally agree. I was aware of some things that you have pointed out, and i did my research on what you said as well. This article was written for mostly the standards geeks that urge to serve as application/xhtml+xml to stick with W3C standards, and hasn’t much knowledge in PHP to get the thing done.

    Anyway, with my experience, this works with most of the browsers nowadays (FireFox, Opera, Mozilla, etc) and some previous versions of most of the commonly used browsers.

    I’m looking forward to publishing an article based on ‘Accept’ header in the near future, with all thanks to you.

  3. I think php is hard enough to test without needing to get parse errors when you echo variables…

  4. Alex Caro says:

    Venushka: Thanks for this article Venushka, it helped me to add the application/xhtml+xml header to a PHP page. I already knew how to, but it never occured to me that the code to do this should be put at the beginning of the page. Thanks again! ^^

    To Jim: I didn’t understand your explaination. Could someone explain what he meant and possiblely give an example?

    Also, I know this is an old article, but thanks for any help in advance to my question.

leave a reply

Please feel free to leave a comment about this post.

(required)
(will not be published ,but required)