Jeroen Bernsen's Blog
A blog about using Microsoft .NET Development Technology

Choosing HTML or XHTML in Microsoft development Environments

20 October 2009 14:19 by Administrator

General overview


Currently HTML 4.01 and XHTML 1.0 & 1.1 are the current standards for web page development. The successors HTML 5, XHTML 2 (not backwards compatible) and XHTML 5 (part of HTML 5) are still under development and are not yet ready for stable use.

So what to choose: HTML v4.01 versus XHTML 1.x ?

 

Advantages of XHTML:

  • Documents are consistent and well-formed according to XML standards. 
  • Documents can be processed with standard XML tooling (like XSLT transformations). 
  • Incorrect XHTML won’t display and authors are forced to correct it. This can also be a disadvantage since your page won’t show at all.

Advantages of HTML:

  • Page size can be slightly less when using HTML because of no mandatory quotations marks, more optional elements and optional closing tags. This can only be a valid reason when you are expecting huge amounts of traffic.
  • HTML is automatically corrected by the browser so the user always sees as much as possible with incorrect pages. The disadvantage is that the author doesn’t notice and every browsers can correct errors in differents ways giving different results.

Furthermore you should know that XHTML should be served with a mime-type of application/xhtml+xml BUT Internet explorer doesn’t accept this (however there are tricks to get around this).

Scenario’s for definitive XHTML usage:

  • Your backend system delivers your data in XML. You can use XSLT to transform it to webpages.

Scenario’s for defintive HTML usage:

  • The site will have huge traffic and every byte counts.

As a rule of thumb I would recommend the following:

  • Write your pages in XHTML 1.0. Your documents will be consistent & well-formed.
  • Serve your pages with mime-type application/html.  They will be rendered as HTML, this will work with Internet Explorer and other browsers and it will also make sure that invalid syntax will be corrected by the browser (this should not happen, but the users should not be bothered with it).
  • Validate all your pages using tooling (e.g. W3C Markup validation service, HTML Validator extension for Firefox, etc) to make sure they are valid.

This gives you best of  both worlds. Furthermore ASP.NET renders XHTML 1.0 conformant output (XHTML 1.0 Transitional is the default).

Note: XHTML 1.1 is very strict and only allows mime-type application/xhtml+xml which does not work with IE.

Using the right DOCTYPE


The doctype (first item in your page) tells the browser which markup versions is used (see http://hsivonen.iki.fi/doctype/) for more information.
Basically when using HTML 4.01 or XHTML 1.0 you can choose between the following flavours:

  • Strict, no presentation markup allowed, and no deprecated elements allowed.
  • Transitional, presentation elements are allowed (e.g. font). Framesets are not allowed.
  • Frameset, presentation elemens and framesets are allowed.

Note XHTML 1.1 is always strict.

As a rule of thumb I would recommend using strict. It’s good practise to separate data and presentation. You will have to do your presentation using CSS.

Using XHTML in Microsoft Environments


XHTML 1.0 Transitional is the default mode in ASP.NET and it’s also the default doctype in Visual Studio web form templates. For using XHTML 1.0 Strict you’ll have to do the following:

Note: if you want to go for XHTML 1.1 notice that ASP.NET does not always render XHTML 1.1 conformant, see http://msdn.microsoft.com/en-us/library/exc57y7e.aspx.

When using ASP.NET MVC you will have complete control over the generated output and XHTML strict should be no problem.

Comments