Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > CLIENT SIDE CODING > XML TUTORIALS > XHTML: THE POWER OF TWO LANGUAGES


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

XHTML: The power of two languages
By Sathyan Munirathinam - 2004-01-09 Page:  1 2 3 4 5 6 7

XHTML validation rules

An XHTML document must be well-formed XML. It must conform to basic XML syntax:

Tag and attribute names must be written in lower-case.

HTMLXHTML
<TD BGCOLOR="#ffcc33"><td bgcolor="#ffcc33">

Elements must nest; no overlapping. With XML and XHTML, you need to close the tags in reverse order -- in other words: last opened, first closed.

HTMLXHTML
<p>Be <b>bold!</p></b><p>Be <b>bold!</b></p>

All non-empty elements must be closed. For example, with HTML, many people use the <p> tag to separate paragraphs. This tag is designed to mark the beginning and (with the closing </p> tag) end of a paragraph. That makes it a non-empty tag since it contains the paragraph text.

HTMLXHTML
First paragraph<p> Second paragraph<p> <p>First paragraph</p> <p>Second paragraph</p>

Affected elements: <basefont>, <body>, <colgroup>, <dd>, <dt>, <head>, <html>, <li>, <p>, <tbody>, <thead>, <tfoot>, <th>, <td>, <tr>.

Empty elements must be terminated. All empty elements must use the XML empty tag syntax with a trailing forward slash before the end bracket (for example, <br> becomes <br />). Note the space after the element text and the closing delimiter, />. This is for compatibility with current browsers.

HTMLXHTML
<hr><hr />
<br><br />
<input ... ><input ... />
<param ... ><param ... />
<img src="valid.gif"><img src="valid.gif" />

Affected elements: <area>, <base>, <br>, <col>, <frame>, <hr>, <img>, <input>, <isindex>, <link>, <meta>, <option>, <param>.

Attribute values must be quoted. No more <img ... border=0>. You now need to put quotes around every attribute, even if it's numeric.

HTMLXHTML
<img ... border=0><img ... border="0" />

Attribute value pairs cannot be minimized. No stand alone attributes (also known as minimized attributes) are allowed. For example, <option selected> is no longer valid. Instead, you must use <option selected="selected">.

Inline tags cannot contain block-level tags. For example, an anchor tag cannot enclose a table.

Scripting elements pose a problem for XHTML compatibility. The XML parser will parse the script as an XML document unless you enclose your script in a CDATA block. Therefore, a JavaScript element would now look like this:


<script type="text/javascript">
<![CDATA[ alert("hello"); ]]>
</script>

This can be a problem for most current browsers, as they do not like the CDATA block. For now, the only solution is to call the JavaScript from an external file. For example:


<script language="JavaScript" type="text/javascript" src="main.js"></script> 

For server-side programmers, this can be a problem when the JavaScript is modified dynamically. Using a separate file source for your JavaScript prevents you from being able to dynamically change your JavaScript. Because the JavaScript is included on the client side, the server side isn't able to touch it. When modifying JavaScript using ASP, JSP, or PHP scripting, use the standard HTML method of script declaration. This is the one place where making JSP or ASP 100% compatible with XHTML will be most problematic. Remember, however, the goal is not to be 100% compatible with XHTML, but to begin incorporating XHTML where feasible, allowing a quick and easy transition when that is necessary. When that time arrives, new compatible browsers should be available and you'll be set to make the jump to 100% compatibility.



View XHTML: The power of two languages Discussion

Page:  1 2 3 4 5 6 7 Next Page: XHTML Basic to replace CHTML and WML

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.