2006-03-23

How to write XHTML even if you don't know how

Warning to RSS users: this post may not be legible in newsreaders that don't understand Atom very well.

  1. Put all tag names and attribute names in lower case.
  2. Make sure every start-tag has an end-tag. This rule does not apply to the HTML empty tags, namely basefont, br, area, link, img, param, hr, input, col, frame, and isindex. (If you don't know what some of these are, don't worry about it).
  3. Replace the > at the end of an empty tag with the three-character sequence " />".
  4. Make sure all start-tags and end-tags are properly nested.
  5. Make sure all attribute values are in quotation marks, either single or double.
  6. Make sure attributes like "checked", that don't have values, are written "checked='checked'".
  7. Any & and < characters, even in scripts or stylesheets, must be replaced by &amp; and &lt; respectively.
  8. Don't wrap scripts in comment markers (<!-- ... -->).
  9. Make sure you use the semicolon after an entity reference like &aacute;.

That's all.

4 comments:

Anonymous said...

livejournal syndication sure did doubly unescape your html entities: http://syndicated.livejournal.com/recycledknowled/41574.html

Stephen said...

I don't understand 6?

Wolf550e said...

Stephen: in html forms, you sometimes have stuff like:
<form>
<input type="checkbox" name="subscribe">I want to get spam from y'all
</form>

and to make the checkbox already checked when the page loads, you do:
<form>
<input type="checkbox" name="subscribe" checked>I want to get spam from y'all
</form>

So in XHTML, since every attribute has a value, you must do:
<form>
<input type="checkbox" name="subscribe" checked="checked">I want to get spam from y'all
</form>

Same thing with "selected" on "option" elements inside the "select" (drop down box), and others like them.

HTH

Anonymous said...

3. Replace the > at the end of an empty tag with the three-character sequence " />".

Don't do that. A compliant HTML parser will eat your document.

Believe it or not, the actual meaning of that sequence is close the tag that contains the single-tag element and emit a > to the containing document.