当前位置: 移动技术网 > IT编程>网页制作>CSS > XHTML?它与 HTML的区别?如何转换

XHTML?它与 HTML的区别?如何转换

2020年04月01日  | 移动技术网IT编程  | 我要评论

闻道华东seo,清音浊世录,儿童套房

什么是 xhtml?xhtml 是以 xml 格式编写的 html。

  • xhtml 指的是可扩展超文本标记语言
  • xhtml 与 html 4.01 几乎是相同的
  • xhtml 是更严格更纯净的 html 版本
  • xhtml 是以 xml 应用的方式定义的 html
  • xhtml 是 2001 年 1 月发布的 w3c 推荐标准
  • xhtml 是大小写敏感的,标准的 xhtml 标签应该使用小写。
  • xhtml 得到所有主流浏览器的支持

为什么使用 xhtml?因特网上的很多页面包含了"糟糕"的 html。如果在浏览器中查看,下面的 html 代码运行起来非常正常(即使它并未遵守 html 规则):

1 <html>
2  <head>
3  <title>this is bad html</title>
4  <body>
5  <h1>bad html
6  <p>this is a paragraph
7  </body>

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)。<html>, <head>, <title>, 和 <body> 元素也必须存在,并且必须使用 <html> 中的 xmlns 属性为文档规定 xml 命名空间。下面的例子展示了带有最少的必需标签的 xhtml 文档:

 1 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
 2  "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
 3 
 4  <html xmlns="http://www.w3.org/1999/xhtml">
 5 
 6  <head>
 7  <title>title of document</title>
 8  </head>
 9 
10  <body>
11 ...... 
12  </body>
13 
14  </html>

xhtml 元素必须合理嵌套
在 html 中,一些元素可以不互相嵌套,像这样:

1 <b><i>this text is bold and italic</b></i>

在 xhtml 中,所有的元素都必须互相合理地嵌套,像这样:

1 <b><i>this text is bold and italic</i></b>

xhtml 元素必须有关闭标签

1 错误示例:
2 <p>this is a paragraph
3 <p>this is another paragraph
1 正确示例:
2 <p>this is a paragraph</p>
3 <p>this is another paragraph</p>

空元素必须包含关闭标签

1 错误示例:
2 a break: <br>
3  a horizontal rule: <hr>
4 an image: <img src="happy.gif" alt="happy face">
5 正确示例:
6 a break: <br />
7  a horizontal rule: <hr />
8  an image: <img src="happy.gif" alt="happy face" />

xhtml 元素必须是小写

 1 错误示例:
 2 
 3 <body>
 4  <p>this is a paragraph</p>
 5  </body>
 6 正确示例:
 7 
 8 <body>
 9  <p>this is a paragraph</p>
10  </body>

属性名称必须是小写

1 错误示例:
2 
3 <table width="100%">
4 正确示例:
5 
6 <table width="100%">

属性值必须有引号

1 错误示例:
2 <table width=100%>
3 正确示例:
4 <table width="100%">

不允许属性简写

 1 错误示例:
 2 
 3 <input checked>
 4  <input readonly>
 5  <input disabled>
 6  <option selected>
 7 正确示例:
 8 
 9 <input checked="checked">
10  <input readonly="readonly">
11  <input disabled="disabled">
12  <option selected="selected">

如何将 html 转换为 xhtml?

  1. 添加一个 xhtml <!doctype> 到你的网页中
  2. 添加 xmlns 属性添加到每个页面的html元素中。
  3. 改变所有的元素为小写
  4. 关闭所有的空元素

  5. 修改所有的属性名称为小写
  6. 所有属性值添加引号

推荐网址:

  1. 使用 w3c 验证器来测试你的 xhtml
  2. xml学习教程
  3. xhtml学习教程

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网