Xhtml 简明教程
XHTML - Tips & Tricks
本章列出了编写 XHTML 文档时应了解的各种提示和技巧。这些提示和技巧可帮助您创建高效的文档。
This chapter lists out various tips and tricks which you should be aware of while writing an XHTML document. These tips and tricks can help you create effective documents.
Tips for Designing XHTML Document
以下是设计 XHTML 文档的一些基本准则 −
Here are some basic guidelines for designing XHTML documents −
Design for Serving and Engaging Your Audience
在考虑满足受众需求时,您需要设计高效且引人注目的文档,以达到此目的。您的文档应便于查找所需信息,并提供一个熟悉的环境。
When you think of satisfying what your audience wants, you need to design effective and catchy documents to serve the purpose. Your document should be easy for finding required information and giving a familiar environment.
例如,学术界人士或医学从业者可以使用包含长句子、复杂图表、特定术语等类似日志的文档,而面向在校儿童的文档必须简单且提供信息。
For example, Academicians or medical practitioners are comfortable with journal-like document with long sentences, complex diagrams, specific terminologies, etc., whereas the document accessed by school-going children must be simple and informative.
Inside the XHTML Document
以下是有关 XHTML 文档中元素的一些提示 −
Here are some tips regarding elements inside the XHTML document −
The XML Declaration
并非所有 XHTML 文档都需要 XML 声明,但极力建议 XHTML 文档的作者在所有文档中使用 XML 声明。当文档的字符编码不是默认 UTF-8 或 UTF-16 时,需要此类声明。
An XML declaration is not required in all XHTML documents but XHTML document authors are strongly encouraged to use XML declarations in all their documents. Such a declaration is required when the character encoding of the document is other than the default UTF-8 or UTF-16.
Empty Elements
它们在空元素的尾随 / 和 > 前包含一个空格。例如,<br />, <hr /> 和 <img src="/html/xhtml.gif" alt="xhtml" />。
They include a space before the trailing / and > of empty elements. For example, <br />, <hr />, and <img src="/html/xhtml.gif" alt="xhtml" />.
Embedded Style Sheets and Scripts
如果样式表使用 “<”、“&”、“]]>” 或 “—”,请使用外部样式表。
Use external style sheets if your style sheet uses “<”, “&”, “]]>”, or “—”.
如果脚本使用 “<”、“&”、“]]>” 或 “—”,请使用外部脚本。
Use external scripts if your script uses “<”, “&”, or “]]>”, or “—”.
Line Breaks within Attribute Values
避免属性值内出现换行符和多个空白字符。不同浏览器对此的处理不一致。
Avoid line breaks and multiple whitespace characters within attribute values. These are handled inconsistently by different browsers.
Isindex Element
文档的文档头中不得包含多个 isindex 元素。isindex 元素已被弃用,取而代之的是 input 元素。
Do not include more than one isindex element in the document head. The isindex element is deprecated in favor of the input element.
The lang and xml:lang Attributes
指定元素语言时,同时使用 lang 和 xml:lang 属性。xml:lang 属性的值优先。
Use both the lang and xml:lang attributes while specifying the language of an element. The value of the xml:lang attribute takes precedence.
Element Identifiers
XHTML 1.0 已弃用 a、applet、form、frame、iframe、img 和 map 元素的 name 属性。后续版本中将从 XHTML 中删除这些属性。因此,开始使用 id 元素进行元素标识。
XHTML 1.0 has deprecated the name attributes of a, applet, form, frame, iframe, img, and map elements. They will be removed from XHTML in subsequent versions. Therefore, start using id element for element identification.
Using Ampersands in Attribute Values
应当将&符号(“&”)表示为实体引用&。
The ampersand character ("&") should be presented as an entity reference &.
Example
<!-- This is invalid in XHTML -->
http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user.
<!-- Correct XHTML way of writing this is as follows -->
http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user
Whitespace Characters in HTML and XML
HTML 文档中合法的某些字符在 XML 文档中是非法的。例如,在 HTML 中,换页符字符(U+000C)被当作空白处理,而在 XHTML 中,由于 XML 中的角色定义,它是非法的。
Some characters that are legal in HTML documents are illegal in XML document. For example, in HTML, the form-feed character (U+000C) is treated as white space, in XHTML, due to XML’s definition of characters, it is illegal.
Named Character Reference &Apos;
已在 XML 1.0 中引入了命名字符引用”(即撇号,U+0027),但它未出现在 HTML 中。因此,Web 开发人员应使用 ' 而不是 ',以期按照 HTML 4 Web 浏览器的预期工作。
The named character reference ' (the apostrophe, U+0027) was introduced in XML 1.0 but does not appear in HTML. Web developers should therefore use ' instead of ' to work as expected in HTML 4 Web Browsers.