XHTML Introduktion

XHTML 是以 XML 格式编写的 HTML。

什么是 XHTML?

  • XHTML 指的是可扩展超文本标记语言
  • XHTML 与 HTML 4.01 几乎是相同的
  • XHTML 是更严格更纯净的 HTML 版本
  • XHTML 是以 XML 应用的方式定义的 HTML
  • XHTML 是 2001 年 1 月发布的 W3C 推荐标准
  • XHTML 得到所有主流浏览器的支持

为什么使用 XHTML?

因特网上的很多页面包含了“糟糕”的 HTML。

如果在浏览器中查看,下面的 HTML 代码运行起来非常正常(即使它并未遵守 HTML 规则):

<html>
<head>
<title>This is bad HTML</title>
<body>
<h1>Bad HTML
<p>This is a paragraph
</body>

XML 是一种必须正确标记且格式良好的标记语言。

如果希望学习 XML,请阅读我们的 XML 教程

今日的科技界存在一些不同的浏览器技术。其中一些在计算机上运行,而另一些可能在移动电话或其他小型设备上运行。小型设备往往缺乏解释“糟糕”的标记语言的资源和能力。

所以 - 通过结合 XML 和 HTML 的长处,开发出了 XHTML。XHTML 是作为 XML 被重新设计的 HTML。

与 HTML 相比最重要的区别:

文档结构

  • XHTML DOCTYPE 是强制性的
  • <html> 中的 XML namespace 属性是强制性的
  • <html>、<head>、<title> 以及 <body> 也是强制性的

元素语法

  • XHTML 元素必须正确嵌套
  • XHTML 元素必须始终关闭
  • XHTML 元素必须小写
  • XHTML 文档必须有一个根元素

属性语法

  • XHTML 属性必须使用小写
  • XHTML 属性值必须用引号包围
  • XHTML 属性最小化也是禁止的

!DOCTYPE ....> 是强制性的

XHTML 文档必须进行 XHTML 文档类型声明(XHTML DOCTYPE declaration)。

您可以在 CodeW3C.com 的标签参考手册中找到完整的 XHTML 文档类型

、<head>、<title> 以及 <body> 元素也必须存在,并且必须使用 <html> 中的 xmlns 属性为文档规定 xml 命名空间。

The following example shows an XHTML document with the minimum required tags:

<!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">
<head>
<title>Title of document</title>
</head>
<body>
......
</body>
</html>

How to convert from HTML to XHTML

  1. Add XHTML <!DOCTYPE> to the first line of each page
  2. Add the xmlns attribute to the html element of each page
  3. Change all element names to lowercase
  4. Close all empty elements
  5. Change all attribute names to lowercase
  6. Add quotes to all attribute values

Verify your XHTML with the W3C validator

Enter your website URL in the text box below:

XHTML test

This test contains 20 questions and no time limit.

This test is informal and is a good way to understand your level of knowledge of XHTML.

Each correct answer is worth 1 point. After the test is over, your total score will be displayed. The maximum score is 20 points.

Start the XHTML test