XML technologies / XML / XML in a Nutshell / XML on the web / XHTML

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

XHTML is an official W3C Recommendation defining an XML-compatible HTML—or, more precisely, redefining HTML as an XML application instead of an SGML application. Merely looking at a document, you might not notice anything different. It uses the familiar tags <p>, <li>, <table>, <h1> and so on. Elements and attributes have the same names as HTML, and syntax is essentially unchanged.

The difference is less what is allowed than what is forbidden. <p> is a valid XHTML tag, but <P> is forbidden. <table border="0" width="515"> is valid XHTML; <table border=0 width=515> is not. A paragraph starting with <p> and ending with </p> is valid, but one lacking the closing tag </p> is not. Most HTML documents need substantial work before becoming well-formed, valid XHTML. Once valid XHTML, they are also valid XML, processable with the same editors, parsers and other XML tools.

From HTML to XHTML

Most changes required to convert existing HTML concern document well-formedness. An old HTML document probably needs at least these changes:

  • Add missing end-tags, such as </p> and </li>.

  • Rewrite overlapping elements to nest properly. Change <p><em>ein betonter Absatz</p></em> to <p><em>ein betonter Absatz</em></p>.

  • Quote attribute values with single or double quotes. Change <p align=center> to <p align="center">.

  • Give abbreviated Boolean attributes values matching their names. Change <input type="checkbox" checked> to <input type="checkbox" checked="checked">.

  • Replace occurrences of & or < in character data or attribute values with &amp; and &lt;. For example, change A&P to A&amp;P and <a href="http://www.google.com/search?client=googlet&q=Java%20XML"> to <a href="http://www.google.com/search?-client=googlet&amp;q=Java%20XML">.

  • Ensure the document has a single html-root element.

  • Change empty elements such as <hr> to <hr/> or <hr></hr>.

  • Add hyphens to comments, changing <! dies ist ein Kommentar> into <!-- dies ist ein Kommentar -->.

  • Encode the document in UTF-8 or UTF-16, or add an XML declaration specifying its character encoding.

XHTML requires validity as well as well-formedness. A valid XHTML document also needs these changes:

  • Add a DOCTYPE declaration referencing one of the three XHTML DTDs.

  • Lowercase all element and attribute names.

  • Adjust markup for validity against the DTD: remove nonstandard elements such as marquee, add required attributes such as the alt attribute of img, and remove children from elements not permitting them, such as blockquote within p.

XHTML adds requirements unnecessary for well-formedness or validity but simplifying parsing:

  • The root element must be html.

  • A DOCTYPE declaration must use a PUBLIC identifier identifying one of the three XHTML DTDs.

Finally, you may optionally add an XML declaration or xmlstylesheet, a processing instruction to the document prolog.

The next example, from O’Reilly’s website, illustrates many web validity problems. It is actually tidier than most pages, yet some attribute values are unquoted. The attribute noshade of HR has no value at all. There is no document type declaration. Tag case varies, mostly uppercase. The DD elements lack end-tags, and text in the second definition is outside any DT or DD.

<HTML>
  <HEAD>
    <TITLE>O'Reilly Shipping Information</TITLE>
  </HEAD>
  <BODY BGCOLOR="#ffffff" VLINK="#0000CC" LINK="#990000" TEXT="#000000">
    <table border=0 width=515>
      <tr>
        <td>
          <IMG SRC="/www/graphics_new/generic_ora_header_wide.gif" BORDER=0>
          <H2>U.S. Shipping Information </H2>
          <HR size="1" align=left noshade>
          <DL>
            <DT><B>UPS Ground Service (Continental US only -- 5-7 business days):</B></DT>
            <DD>
              <PRE>
              $  5.95 - $ 49.99 ......................... $ 4.50
              $ 50.00 - $ 99.99 ......................... $ 6.50
              $100.00 - $149.99 ......................... $ 8.50
              $150.00 - $199.99 ......................... $10.50
              $200.00 - $249.99 ......................... $12.50
              $250.00 - $299.99 ......................... $14.50
              </PRE>
            <DT><B>Federal Express:</B></DT>
            (Shipping within 24 hours of receipt of order by O'Reilly)
            <DD>
              <PRE>
                <EM>1 or 2 books</EM>:
                Economy 2-day ............................. $ 8.75
                Overnight Standard (Afternoon Delivery) ... $12.75
                Overnight Priority (Morning Delivery) ..... $16.50
              </PRE>
          </DL>
          <b>Alaska and Hawaii:</b> add $10 to Federal Express rates.
          <P>
            <A HREF="int-ship.html"><b>International Shipping Information</b></A>
          <P>
          <CENTER>
            <HR SIZE="1" NOSHADE>
            <FONT SIZE="1" FACE="Verdana, Arial, Helvetica">
              <A HREF="http://www.oreilly.com/"><B>O'Reilly Home</B></A> <B> | </B><A HREF="http://www.oreilly.com/sales/bookstores"><B>O'Reilly Bookstores</B></A> <B> | </B><A HREF="http://www.oreilly.com/order_new/"><B>How to Order</B></A> <B> | </B><A HREF="http://www.oreilly.com/oreilly/contact.html"><B>O'Reilly Contacts<BR></B></A><A HREF="http://www.oreilly.com/international/"><B>International</B></A> <B> | </B><A HREF="http://www.oreilly.com/oreilly/about.html"><B>About O'Reilly</B></A> <B> | </B><A HREF="http://www.oreilly.com/affiliates.html"><B>Affiliated Companies</B></A><p>
              <EM>&amp;copy; 2000, O'Reilly Media, Inc.</EM>
            </FONT>
          </CENTER>
        </td>
      </tr>
    </table>
  </BODY>
</HTML>

Example: A typical HTML document

The next example converts it to XHTML, fixing those and other problems. Invalid presentation attributes such as size and noshade on hr had to become CSS styles. Required document type and namespace declarations were also added. Both HTML browsers and XML browsers and parsers can now read it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta name="generator" content="HTML Tidy, see www.w3.org" />
    <style type="text/css">
      body      {backgroundColor: #FFFFFF; color: #000000}
      a:visited {color: #0000CC}
      a:link    {color: #990000}
    </style>
    <title>O'Reilly Shipping Information</title>
  </head>
  <body>
    <table border="0" width="515">
      <tr>
        <td><img src="/www/graphics_new/generic_ora_header_wide.gif" style="border-width: 0" alt="O'Reilly"/>
          <h2>U.S. Shipping Information</h2>
          <hr style="height: 1; text-align: left"/>
          <dl>
            <dt><b>UPS Ground Service (Continental US only -- 5-7 business days):</b></dt>
            <dd>
              <pre>
              $  5.95 - $ 49.99 ......................... $ 4.50
              $ 50.00 - $ 99.99 ......................... $ 6.50
              $100.00 - $149.99 ......................... $ 8.50
              $150.00 - $199.99 ......................... $10.50
              $200.00 - $249.99 ......................... $12.50
              $250.00 - $299.99 ......................... $14.50
              </pre>
            </dd>
            <dt><b>Federal Express:</b></dt>
            <dd>(Shipping within 24 hours of receipt of order by O'Reilly)</dd>
            <dd>
              <pre>
                <em>1 or 2 books</em>:
                Economy 2-day ............................. $ 8.75
                Overnight Standard (Afternoon Delivery) ... $12.75
                Overnight Priority (Morning Delivery) ..... $16.50
              </pre>
            </dd>
          </dl>
          <b>Alaska and Hawaii:</b> add $10 to Federal Express rates.
          <p><a href="int-ship.html"><b>International Shipping Information</b></a></p>
          <div style="font-size: 1; font-face: Verdana, Arial, Helvetica; text-align: center">
            <hr style="height: 1"/>
            <a href="http://www.oreilly.com/"><b>O'Reilly Home</b></a> <b>|</b> <a href="http://www.oreilly.com/sales/bookstores"><b>O'Reilly Bookstores</b></a> <b>|</b> <a href="http://www.oreilly.com/order_new/"><b>How to Order</b></a><b>|</b> <a href="http://www.oreilly.com/oreilly/contact.html"><b>O'Reilly Contacts<br /></b></a> <a href="http://www.oreilly.com/international/"><b>International</b></a> <b>|</b> <a href="http://www.oreilly.com/oreilly/about.html"><b>About O'Reilly</b></a> <b>|</b> <a href="http://www.oreilly.com/affiliates.html"><b>Affiliated Companies</b></a>
          </div>
            <p style="font-size: 1; font-family: Verdana, Arial, Helvetica"><em>&amp;copy; 2000, O'Reilly Media, Inc.</em></p>
        </td>
      </tr>
    </table>
  </body>
</html>

Example: A valid XHTML document

Note: Making these changes in large documents or collections is tedious. Fortunately, an open-source tool handles most work. Dave Ragget’s Tidy is a C program ported to most major operating systems, converting ugly HTML to valid XHTML. To convert bad.html into good.xml, enter:

 % tidy --output-xhtml yes bad.html good.xml 

Tidy repairs what it can and reports what needs manual editing, for example a required alt attribute missing from an img element.

Three XHTML DTDs

XHTML has three forms, depending on the chosen DTD:

Strict

The W3C recommends this form. It includes basic elements and attributes such as p and class, but no deprecated ones such as applet and center. It also forbids presentation attributes such as those on body: bgcolor, vlink, link and text. CSS supplies these functions instead. Strict XHTML uses this DOCTYPE declaration:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd" >

The example A valid XHTML document uses this DTD.

Transitional

A looser form useful when deprecated elements and attributes such as applet and bgcolor cannot easily be abandoned. It uses this DOCTYPE declaration:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd" >

Frameset

Identical to Transitional except that frame elements such as frameset and iframe are allowed. It uses this DOCTYPE declaration:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "DTD/xhtml1-frameset.dtd" >

All three use the same namespace. Choose Strict unless you have a compelling reason otherwise.

Browser support for XHTML

Many browsers, especially IE5.0 and earlier and Netscape 4.79 and earlier, handle XHTML inconsistently. They do not require it, accepting malformed, invalid HTML, but also struggle with common XHTML constructs.

XML declarations and processing instructions

Some display processing instructions and XML declarations inline. Avoid those browsers if possible.

Few respect the XML encoding declaration, and many cannot detect UTF-8 or UCS-2 Unicode text automatically. For non-ASCII encodings, add a meta element to the header identifying the character set:

<meta http-equiv="Content-type" content='text/html; charset="ISO-8859-1"'></meta>

Empty elements

Empty-element syntax varies in browsers. Some understand <hr/> but not <hr></hr>, usually drawing two horizontal rules. Others recognize <hr></hr> but not <hr/>, often omitting the rule entirely. Best results seem to use a single empty-element tag with an optional attribute such as class or id, for example <hr class="empty" />. The class attribute is unnecessary except to keep browsers from overlooking the />. Any DTD-permitted attribute works equally well.

If one instance is empty but the element type permits content, such as a textless p, use two tags like <p></p> rather than a single empty-element tag, <p/>.

Entity references

Embedded scripts often contain reserved characters like & or <, making XML not well-formed. Most JavaScript and VBScript interpreters, however, do not recognize &amp; or &lt; in place of operators. If rewriting without them is impossible—for instance replacing less-than with greater-than or equality and swapping arguments—use external scripts instead.

Most non-XML browsers also fail to recognize the predefined entity reference &apos;. Avoid it where possible and use the literal ' instead. Trouble arises only in single-quoted attribute values containing double quotes. Most browsers recognize &quot; for the character ", so double-quote the value and escape internal quotes with &quot;.

Other unsupported features and properties

Other subtle differences separate browser behavior from correct XHTML. XHTML permits character references and CDATA sections, understood by almost no current browsers. Such constructs rarely appear when converting HTML, and authors creating XHTML can generally avoid them.

Mozilla, Opera 5.0 onward and Netscape 6.0 onward parse and display valid XHTML without these author workarounds. Safari and IE5.5 generally display it if incorrectly labeled text/html. Both are confused by the correct MIME type, application/xhtml+xml. Many users have not upgraded enough, so considerate designers must keep following these guidelines.

  

 

 

 

back to top