In this section we present a simple XHTML example that covers all the XHTML development basics. As previously discussed, all XHTML pages begin with a DOCTYPE declaration to specify what version of XHTML you are using. The DOCTYPE (document type) tells browsers what type of information to expect in the page, which in turn, tells it how to render the page. The DOCTYPE information is also used byXHTML validators to configure the process by which it verifies the syntax of your Web pages. An XHTML example file using the transitional flavor is shown here:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> (Your XHTML page contents goes here - we'll cover this next.) </html>
Whew! That's a lot of XHTML code, and you can save time by bookmarking this page and just copying and pasting the code into your pages. Now let's review:
- Start by typing the XML declaration: <?xml version="1.0" encoding="UTF-8"?>. This is a processing instruction used to declare to the world that this document is an XML document which uses the UTF-8 (Unicode) character encoding. Note that the XML declaration is optional in XHTML.
- Next comes the XHTML transitional DOCTYPE declaration which declares the conformance level of the page.
- The <html xmlns="http://www.w3.org/1999/xhtml"> line opens the XHTML document and declares the default namespace - we'll learn more about XML namespaces in the XML tutorial section; for now just copy and paste that line.
- Next comes the rest of your XHTML page - we'll be discussing what to put here later.
- At the end of the page is the </html> tag which ends the XHTML example.
XHTML Tips and Tricks
Here are some common XHTML gotcha's and frequently asked questions (FAQ's), tips, and tricks:
- XHTML documents start with an <html> tag - there is no such thing as an <xhtml> tag.
- It is required that you declare the XHTML namespace inside the opening <html> tag.
- This XHTML example covered the use of XHTML transitional - for XHTML strict or frameset, use the appropriate DOCTYPE Declaration.
- Remember that declaring a DOCTYPE with a valid identifier at the top of an XHTML page puts most browers in standards mode- so remember not to use old browser hacks, and non-standard tags. (Otherwise, use just use regular HTML)
- For some browsers, including Microsoft Internet Explorer 6, if you start an XHTML page with the XML declaration, the browser goes into quirks mode, an unfortunate bug. The workaround is to delete the optional declaration and declare the the encoding using a meta tag.
- The DOCTYPE declaration has to be in all uppercase characters, just like in the XHTML example code.