XHTML 和 HTML 之间的主要区别是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/195840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:31:14  来源:igfitidea点击:

What are the main differences between XHTML and HTML?

htmlxhtml

提问by alsadk

What are the main differences between XHTML and HTML? Which one is better in your opinion, and why? Do most browsers support both?

XHTML 和 HTML 之间的主要区别是什么?你认为哪个更好,为什么?大多数浏览器都支持吗?

回答by Andy

XHTMLis based on XML, and thus requires the source to be well-formed. Since XHTML is more strict than HTML, less pre-processing is needed by the rendering engine.

XHTML基于XML,因此要求源代码格式正确。由于 XHTML 比 HTML 更严格,渲染引擎需要的预处理更少。

XHTML should be served as application/xhtml+xml for you to take advantage of the benefits, otherwise XHTML will be treated as ordinary HTML. Serving it as 'application/xhtml+xml' is not common on the web due to Internet Explorer, which cannot handle XHTML.

XHTML 应该作为 application/xhtml+xml 为您提供好处,否则 XHTML 将被视为普通的 HTML。由于 Internet Explorer 无法处理 XHTML,因此将其作为“application/xhtml+xml”在网络上并不常见。

回答by Charles Roper

This is probably the best article I've read on the differences and relative merits of each:

这可能是我读过的最好的文章,其中介绍了每种方法的差异和相对优点:

HTML Versus XHTML

HTML 与 XHTML

Which should we use, HTML or XHTML, and why?

我们应该使用哪个,HTML 还是 XHTML,为什么?

There is also a rather technical comparison on the WHATWG wiki.

WHATWG wiki上还有一个相当技术性的比较。

回答by Vincent Robert

The difference is that XHTML is based on XML while HTML is based on SGML.

区别在于 XHTML 基于 XML 而 HTML 基于 SGML。

Browsers use the SGML parser for content sent with the content type text/htmland the XML parser for application/xhtml+xml.

浏览器对使用内容类型发送的内容使用 SGML 解析器,text/htmlapplication/xhtml+xml.

When using the SGML parser, browsers will continue the parsing even when they encounter a syntax error in the file. This is why so many people think they are doing XHTML when they are sending XML-looking files to the browser. This is actually a mistake since this will trigger many parsing errors in the browser and slow the rendering process.

使用 SGML 解析器时,浏览器即使在文件中遇到语法错误也会继续解析。这就是为什么很多人在向浏览器发送类似 XML 的文件时认为他们正在执行 XHTML 的原因。这实际上是一个错误,因为这会在浏览器中触发许多解析错误并减慢渲染过程。

When using the XML parser, browser will stop the parsing when a syntax error is encountered and display an XML error. This is of course only true for browsers that have an XML parser for HTML content, which is not true for Internet Explorer that will only download the file and not display it.

使用 XML 解析器时,浏览器会在遇到语法错误时停止解析并显示 XML 错误。这当然仅适用于具有 HTML 内容的 XML 解析器的浏览器,而对于仅下载文件而不显示文件的 Internet Explorer 则不然。

What should you do then?

那你应该怎么做?

You should use XHTML if you need the syntax checking and the strict structure that XML impose. You should keep in mind that you must be sending your XHTML content to Internet Explorer with the wrong content type with all the issues implied. You should also keep in mind that your document will break whenever an unvalid content is in the file, so you must take great care to sanitize any user input.

如果您需要 XML 强加的语法检查和严格结构,则应该使用 XHTML。您应该记住,您必须使用错误的内容类型将 XHTML 内容发送到 Internet Explorer,并暗示所有问题。您还应该记住,只要文件中包含无效内容,您的文档就会中断,因此您必须非常小心地清理任何用户输入。

You should use HTML for anything else. HTML just works and is a greatly supported standard today. Even the next standard HTML 5 defines an SGML-based syntax so this will last.

您应该将 HTML 用于其他任何事情。HTML 可以正常工作,并且是当今受到极大支持的标准。即使是下一个标准的 HTML 5 也定义了基于 SGML 的语法,所以这将持续下去。

Keep in mind that whatever the format you choose, validating your output is always a good idea to detect syntax error.

请记住,无论您选择哪种格式,验证输出始终是检测语法错误的好主意。

回答by Guido

The Extensible Hypertext Markup Language, or XHTML, is a markup language that has the same depth of expression as HTML, but also conforms to XML syntax.

可扩展超文本标记语言或 XHTML 是一种标记语言,它具有与 HTML 相同的表达深度,但也符合 XML 语法。

XHTML is "the modern version of HTML 4".

XHTML 是“HTML 4 的现代版本”。

More info : wikipediaand W3C

更多信息:维基百科W3C

回答by zappan

It's not about which one is better; the HTML still continues to develop, and HTML5 is on its way (for years, though :) ) and delivers some new elements for easier management of new technologies like multimedia in web pages.

这不是关于哪个更好;HTML 仍在继续发展,HTML5 也在不断发展(虽然已经持续了很多年:)并提供了一些新元素,以便更轻松地管理网页中的多媒体等新技术。

On the other hand, XHTML is about strictness of XML, should we say keeping things clean. If you keep your HTML document well-formed (close every opened element, keep things nested in the tree-form), you're getting the best out of XHTML/XML world and can still be using the HTML format, declaring your documents to be HTML, but still keeping them 'clean' (well-formed). I don't think we should declare each of them 'better'; they can co-exist. It's just about us doing our things the right way.

另一方面,XHTML 是关于 XML 的严格性,我们应该说保持整洁。如果您保持 HTML 文档格式良好(关闭每个打开的元素,将内容嵌套在树形中),您就可以充分利用 XHTML/XML 世界,并且仍然可以使用 HTML 格式,将您的文档声明为是 HTML,但仍然保持它们“干净”(格式良好)。我不认为我们应该宣布他们每个人都“更好”;它们可以共存。这只是关于我们以正确的方式做事。

回答by Halo

As previously stated, XHTML should be (in theory) valid XML. In theory (but not in practice) non-conforming XHTML should not be rendered by the browser.

如前所述,XHTML 应该是(理论上)有效的 XML。理论上(但实际上并非如此)浏览器不应呈现不符合标准的 XHTML。

There is very rarely an advantage to use one over the other, so write whichever you personally prefer - as long as you do it consistently. HTML5 which is going to be the future web-standard will support both XHTML and HTML 4.01-style tags (the prior via optional "XML serialization"), and XHTML2 is looking like it will be pretty much dead in the water with no vendors actively supporting it.

使用其中一个很少有优势,所以写下你个人喜欢的任何一个——只要你坚持这样做。将成为未来 Web 标准的 HTML5 将同时支持 XHTML 和 HTML 4.01 样式的标签(之前通过可选的“XML 序列化”),而 XHTML2 看起来将在没有供应商积极参与的情况下几乎陷入困境支持它。

回答by cic

Summary: use HTML.

总结:使用 HTML。

There are many differences, and many smart people (that know what they are talking about :D) havealreadysummed upmost of the pros and cons.

许多差异,许多聪明的人(也知道他们在谈论:d)已经总结出了大部分的利弊。

回答by Dave Rutledge

An example of a difference is the break tag.

差异的一个例子是中断标记。

<br>

is proper HTML, but in XHTML where the elements need to validate as XML, you must self-close the tag:

是正确的 HTML,但在元素需要验证为 XML 的 XHTML 中,您必须自行关闭标记:

<br />