XHTML語法與HTML語法非常相似,幾乎所有有效的HTML元素在XHTML中都是有效的。但是,在編寫XHTML文檔時,需要格外注意使HTML文檔與XHTML兼容。
以下是編寫新的XHTML文檔或將現有的HTML文檔轉換爲XHTML文檔時要記住的要點−
在XHTML文檔的開頭編寫DOCTYPE聲明。
僅用小寫字母書寫所有XHTML標記和屬性。
正確關閉所有XHTML標記。
正確嵌套所有標記。
引用所有屬性值。
禁止屬性最小化。
用id屬性替換name屬性。
不贊成腳本標記的語言屬性。
下面是對上述XHTML規則的詳細解釋−
DOCTYPE Declaration
所有XHTML文檔的開頭都必須有DOCTYPE聲明。有三種類型的DOCTYPE聲明,在XHTML Doctypes一章中將詳細討論。下面是一個使用DOCTYPE−
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Case Sensitivity
XHTML是區分大小寫的標記語言。所有的XHTML標記和屬性只需要用小寫字母書寫。
<!-- This is invalid in XHTML --> <A Href="/xhtml/xhtml_tutorial.html">XHTML Tutorial</A> <!-- Correct XHTML way of writing this is as follows --> <a href="/xhtml/xhtml_tutorial.html">XHTML Tutorial</a>
在本例中,ref和anchor tagA不是小寫的,因此不正確。
Closing the Tags
每個XHTML標記都應該有一個等價的結束標記,甚至空元素也應該有結束標記。下面是一個示例,顯示了使用標記的有效和無效方法−
<!-- This is invalid in XHTML --> <p>This paragraph is not written according to XHTML syntax. <!-- This is also invalid in XHTML --> <img src="/images/xhtml.gif" >
下面的語法顯示了用XHTML編寫上述標記的正確方法。不同的是,這裡我們把兩個標籤都關好了。
<!-- This is valid in XHTML --> <p>This paragraph is not written according to XHTML syntax.</p> <!-- This is also valid now --> <img src="/images/xhtml.gif" />
Attribute Quotes
必須引用XHTML屬性的所有值。否則,您的XHTML文檔將被假定爲無效文檔。下面是顯示語法的示例−
<!-- This is invalid in XHTML --> <img src="/images/xhtml.gif" width=250 height=50 /> <!-- Correct XHTML way of writing this is as follows --> <img src="/images/xhtml.gif" width="250" height="50" />
Attribute Minimization
XHTML不允許屬性最小化。這意味著您需要顯式地聲明屬性及其值。下面的示例顯示了差異−
<!-- This is invalid in XHTML --> <option selected> <!-- Correct XHTML way of writing this is as follows --> <option selected="selected">
下面是HTML中最小化屬性的列表,以及用XHTML編寫它們的方式−
HTML Style | XHTML Style |
---|---|
compact | compact="compact" |
checked | checked="checked" |
declare | declare="declare" |
readonly | readonly="readonly" |
disabled | disabled="disabled" |
selected | selected="selected" |
defer | defer="defer" |
ismap | ismap="ismap" |
nohref | nohref="nohref" |
noshade | noshade="noshade" |
nowrap | nowrap="nowrap" |
multiple | multiple="multiple" |
noresize | noresize="noresize" |
The id Attribute
id屬性替換name屬性。XHTML不使用name=「name」,而是使用id=「id」。下面的示例演示了−
<!-- This is invalid in XHTML --> <img src="/images/xhtml.gif" name="xhtml_logo" /> <!-- Correct XHTML way of writing this is as follows --> <img src="/images/xhtml.gif" id="xhtml_logo" />
The language Attribute
不推薦使用腳本標記的語言屬性。下面的示例顯示了這種差異;
<!-- This is invalid in XHTML --> <script language="JavaScript" type="text/JavaScript"> document.write("Hello XHTML!"); </script> <!-- Correct XHTML way of writing this is as follows --> <script type="text/JavaScript"> document.write("Hello XHTML!"); </script>
Nested Tags
必須正確嵌套所有XHTML標記。否則,您的文檔將被假定爲不正確的XHTML文檔。下面的示例顯示語法−
<!-- This is invalid in XHTML --> <b><i> This text is bold and italic</b></i> <!-- Correct XHTML way of writing this is as follows --> <b><i> This text is bold and italic</i></b>
Element Prohibitions
以下元素中不允許有任何其他元素。這項禁令適用於所有深度的築巢。意思是,它包含所有的降序元素。
Element | Prohibition |
---|---|
<a> | Must not contain other <a> elements. |
<pre> | Must not contain the <img>, <object>, <big>, <small>, <sub>, or <sup> elements. |
<button> | Must not contain the <input>, <select>, <textarea>, <label>, <button>, <form>, <fieldset>, <iframe> or <isindex> elements. |
<label> | Must not contain other <label> elements. |
<form> | Must not contain other <form> elements. |
A Minimal XHTML Document
下面的示例向您展示了XHTML1.0文檔的最小內容;