HTML和XHTML有什么区别? [英] What is the difference between HTML and XHTML?

查看:146
本文介绍了HTML和XHTML有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这应该是此问题的规范文章.已经存在许多答案,但是各种差异的描述散布在各地,而且通常还会对我应该使用哪个"提供意见,在此我将不再赘述.
如果您有更多问题要问,或者您知道更多不同之处,请随时进行编辑.

XHTML和HTML有什么区别? XHTML不仅仅是HTML的更严格版本吗?如果XHTML的行为相同,又为什么会有不同版本的XHTML?

What is the difference between XHTML and HTML? Isn't XHTML merely a more strict version of HTML? And why are there different versions of XHTML if they all act the same?

推荐答案

HTML和XHTML有什么区别?

What is the difference between HTML and XHTML?

有很多区别.主要的一个是XHTML是XML文档中的HTML,并且XML具有不同的语法规则:

There are many differences. The main one is that XHTML is HTML in an XML document, and XML has different syntax rules:

  • 默认情况下,XML具有不同的命名空间,因此您必须在XHTML文档中显式使用HTML命名空间xmlns="http://www.w3.org/1999/xhtml"
  • XML区分大小写,标记名称和属性,甚至十六进制字符引用中的x,都必须使用小写字母
  • XML没有可选的开始标签和结束标签,因此您必须全部写出所有标签
  • 同样,XML没有void标签,因此您必须自己用斜杠关闭每个void元素.
  • 可以将没有内容的非空元素写为XML中的单个空元素标记.
  • XML可以包含CDATA节,即用<![CDATA[ .. ]]>分隔的纯文本节; HTML无法
  • 另一方面,XML中没有CDATA或PCDATA元素或属性,因此您必须在任何地方转义<符号(CDATA部分中除外)
  • 在XML中,属性值周围的引号不是可选的,并且没有属性最小化(仅名称属性)
  • XML解析器不像HTML解析器那样宽容错误.
  • XML has a different namespace by default, so you'll have to use the HTML namespace, xmlns="http://www.w3.org/1999/xhtml" explicitly in an XHTML document
  • XML is case sensitive and you'll have to use lowercase for tag names and attributes and even the x in hexadecimal character references
  • XML doesn't have optional start and end tags, so you'll have to write out all of them in full
  • Likewise, XML doesn't have void tags, so you'll have to close every void element yourself with a slash.
  • Non-void elements that have no content can be written as a single empty element tag in XML.
  • XML can contain CDATA sections, sections of plain text delimited with <![CDATA[ .. ]]>; HTML cannot
  • On the other hand, there are no CDATA or PCDATA elements or attributes in XML, so you'll have to escape your < signs everywhere (except in CDATA sections)
  • Quotes around attribute values are not optional in XML, and there is no attribute minimization (name-only attributes)
  • And the XML parser is not as forgiving of errors as the HTML parser.

然后有一些与XML不相关的区别:

Then there are a couple of not XML-related differences:

  • XHTML文档始终以标准模式呈现,而不是怪癖模式
  • XHTML不会在头部查看元命令来确定编码.实际上,W3C验证器在XHTML5文件中将<meta http-equiv="content-type" ...标记为错误,但在HTML5文件中没有.
  • 更早之前,XHTML 1.0严格版和dtd 4.0严格版之间的dtds不匹配会导致验证问题. XTHML 1.0的定义缺少<img><form>上的name属性.不过,这是一个错误,已在XHTML 1.1中修复.
  • XHTML documents are always rendered in standards mode, never in quirks mode
  • XHTML does not look at meta commands in the head to determine the encoding. In fact, the W3C validator flags <meta http-equiv="content-type" ... as an error in XHTML5 files, but not in HTML5 files.
  • Earlier on, mismatches between the dtds for XHTML 1.0 strict and HTML 4.01 strict lead to validation issues. The definition for XTHML 1.0 was missing the name attribute on <img> and <form>. This was an error though, fixed in XHTML 1.1.

请注意,XHTML文档应以正确的文件类型(即 .xhtml 文件扩展名或 application/xhtml + xml MIME类型)提供.您实际上不能在HTML文档中包含XHTML,因为浏览器不会仅通过文件类型来查看内容,从而无法区分这两种语法.
换句话说,如果您有一个HTML文件,则无论文件中是否包含有效的XML,其内容都是HTML.

Note that XHTML documents should be served up with the correct file type, i.e. a .xhtml file extension or an application/xhtml+xml MIME type. You can't really have XHTML in an HTML document, because browsers don't differentiate between the two syntaxes by looking at the content, only by file type.
In other words, if you have an HTML file, its contents are HTML, no matter if it has valid XML in it or not.

值得一提的语法规则要点是标签名称的大小写.尽管HTML文档不区分大小写,但标记名称实际上由DOM公开为大写.这意味着在HTML下,像console.log(document.body.tagName);这样的JavaScript命令将输出"BODY",而在XHTML下,相同的命令将输出"body".

One point about the syntax rules worth mentioning is the casing of tag names. Although HTML documents are case-insensitive, the tag names are actually exposed as uppercase by the DOM. That means that under HTML, a JavaScript command like console.log(document.body.tagName); would output "BODY", whereas the same command under XHTML would output "body".

XHTML不仅仅是HTML的更严格版本吗?

Isn't XHTML merely a stricter version of HTML?

否; XML具有与HTML不同的规则,但不一定严格.如果有的话,XML的规则会更少!

No; XML has different rules than HTML, but it's not necessarily stricter. If anything, XML has fewer rules!

在HTML中,许多功能是可选的.您可以选择是否在属性值两边加上引号;在XML中,您别无选择.在HTML中,您必须记住何时可以选择以及何时不选择:<a href=http://my-website.com/?login=true>中的引号是否可选?在XML中,您不必考虑这一点. XML更容易.

In HTML, many features are optional. You can choose to put quotes around attribute values or not; in XML you don't have that choice. And in HTML, you have to remember when you have the choice and when you don't: are quotes optional in <a href=http://my-website.com/?login=true>? In XML, you don't have to think about that. XML is easier.

在HTML中,某些元素被定义为原始文本元素,即,包含纯文本而不是标记的元素.
还有一些其他元素是可转义的原始文本元素,其中将解析诸如&#233;之类的引用,但将诸如<b>bold</b><!-- comment -->之类的内容视为纯文本.如果您能记住那些是哪些元素,则不必转义<符号(尽管可以选择). XML没有这些内容,因此没有什么可记住的,并且所有元素都具有相同的内容类型.

In HTML, some elements are defined as raw text elements, that is, elements that contain plain text rather than markup.
And some other elements are escapable raw text elements, in which references like &#233; will be parsed, but things like <b>bold</b> and <!-- comment --> will be treated as plain text. If you can remember which elements those are, you don't have to escape < signs (you optionally can though). XML doesn't have that, so there's nothing to remember and all elements have the same content type.

XML具有处理器指令,其中最著名的是序言<?xml version="1.0" encoding="windows-1252"?>中的xml声明.这将告诉浏览器使用哪个XML版本(顺便说一句,1.0是唯一可用的版本)以及哪个字符集.

XML has processor instructions, the most well known of which is the xml declaration in the prolog, <?xml version="1.0" encoding="windows-1252"?>. This tells the browser which version of XML is used (1.0 is the only version that works, by the way) and which character set.

XML以不同的方式解析注释.例如,HTML注释不能以<!-->开头(以>作为第一个字符); XHTML注释可以.
说到注释,使用XHTML可以使用<!-- comment -->注释掉<script><style>元素内的代码块.不要在HTML中尝试. (由于兼容性问题,也不建议在XHTML中使用,但是可以.)

And XML parses comments in a different way. For example, HTML comments can't start with <!--> (with a > as the first character inside); XHTML comments can.
Speaking of comments, with XHTML you can comment out blocks of code inside <script> and <style> elements using <!-- comment -->. Don't try that in HTML. (It's not recommended in XHTML either, because of compatibility issues, but you can.)

如果它们的行为相同,为什么会有不同版本的XHTML?

Why are there different versions of XHTML if they all act the same?

他们没有!例如,在XHTML 1.1中,您可以引用诸如&eacute;&nbsp;之类的字符实体,因为这些实体是在DTD中定义的.当前版本的XHTML(以前称为XHTML5)没有DTD,因此您必须使用数字引用,在这种情况下,请使用&#233;&#160;(或在DOCTYPE声明中自行定义这些实体).毕竟是可扩展的.

They don't! For instance, in XHTML 1.1 you can refer to character entities like &eacute; and &nbsp;, because those entities are defined in the DTD. The current version of XHTML (formerly known as XHTML5) does not have a DTD, so you will have to use numerical references, in this case &#233; and &#160; (or, define those entities yourself in the DOCTYPE declaration. The X means eXtensible after all).

这篇关于HTML和XHTML有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆