Tough guys use XHTML
- 17 May, 2004 19:59
Three Web developers were sitting around arguing over what the most abundant thing on the Internet is.
The first one said, "It must be free music."
The second one said, "Nahh...it's got to be spam e-mail."
The third one said, "It has to be porn."
About that time, a Linux user overheard the conversation and decided to throw in his two cents. "All three of you are wrong. The most abundant thing on the Internet is bad code."
XHTML, when created with the aid of a decent validation tool, helps enforce a stricter set of coding standards than most Web pages adhere to. The following can be said about an XHTML document: it is well-formed, it is both HTML and XML at the same time; and it is an HTML document that adheres to XML syntax rules.
To be valid, an XHTML document must have:
-- A Document Type Definition
-- A closing tag for every tag opened
-- Proper nesting of tags
-- A reference to the XHTML namespace in the opening tag
-- Element and attribute names in lower case (standard HTML is not case-sensitive, but XML is)
-- Double quotes around all attribute values
Since the Document Type Definition (DTD) is the first line in the file, we'll start with that. An XHTML document may be defined as Strict, Transitional or Frameset.
Strict is what the name implies -- a DTD for tough guys or gals who like to do things right (like us). Transitional allows you to use deprecated tags if you really want to crud up your code with presentation elements instead of just using CSS. Frameset is also self-explanatory -- the Document Type of a frame layout. Specifying the DTD is important because it lets the validation tool know what standard to hold your code to. The Strict DTD tells the validation tool to be the mean professor, the one who throws your paper back at you with red ink everywhere.
Initially our page will look something like this:
Try running this through the validation service found at http://www.htmlhelp.com/tools/validator/upload.html. No errors. So far the mean professor has nothing on us.
Now let's try adding a paragraph containing a single image to the page, inserting the following between the body tags:
If you tried to validate that, you just got busted. The image tag wasn't closed. So how do we deal with elements that traditionally haven't had closing tags? Like this:
The slash at the end of the tag closes the element. It is the equivalent of adding a tag, but slightly more elegant and commonly used. Line breaks are another one that will getcha every time:
Whether you're using XHTML or just plain HTML, setting up your pages in such a way that they can be validated against the standard you're aiming for is the most important step.
Cooney is a managing partner and chief software architect for Rivervine Technologies Inc. Contact: email@example.com.