简体   繁体   English

为什么要在 HTML 文件上放置 XHTML doctype 声明? 那有什么作用?

[英]Why put an XHTML doctype declaration on HTML files? What does that do?

I wonder about the number of web pages I encounter that are HTML files, but that wear an XHTML DOCTYPE declaration.我想知道有多少网页是 HTML 文件,但带有 XHTML DOCTYPE 声明。
Why are people doing this?人们为什么要这样做? What do they hope to achieve?他们希望达到什么目标? Why not reserve the XHTML doctype declaration for actual XHTML files?为什么不为实际的 XHTML 文件保留 XHTML doctype 声明?

Or am I missing something?或者我错过了什么?

Edit : there is some confusion about what "actual XHTML files" are;编辑:关于“实际 XHTML 文件”是什么有一些混淆; to demonstrate that the difference is not caused by the DOCTYPE declaration, compare this file to this one .证明的差别不是由DOCTYPE声明引起的,比较这个文件这一个 The first is HTML, the second is XHTML, although the contents are identical;第一个是HTML,第二个是XHTML,虽然内容是一样的; only the file types differ.只有文件类型不同。 Both display fine in compliant browsers, but the first one is parsed with the HTML parser and the second one with the XML parser.两者在兼容浏览器中都能正常显示,但第一个使用 HTML 解析器解析,第二个使用 XML 解析器解析。

Why put an XHTML doctype declaration on HTML files?为什么要在 HTML 文件上放置 XHTML doctype 声明? What does that do?那有什么作用?

All that does is tell markup validators that they're about to validate an XHTML document, as opposed to a regular, SGML-rooted, HTML document.所做的只是告诉标记验证器他们将要验证 XHTML 文档,而不是常规的、基于 SGML 的 HTML 文档。 It describes the content , or more specifically the markup that follows, but nothing else.它描述了内容,或者更具体地说是后面的标记,但仅此而已。

Why are people doing this?人们为什么要这样做? What do they hope to achieve?他们希望达到什么目标? Why not reserve the XHTML doctype declaration for actual XHTML files?为什么不为实际的 XHTML 文件保留 XHTML doctype 声明?

Or am I missing something?或者我错过了什么?

Kind of.的种类。 What actually happened was that people weren't aware that just putting an XHTML doctype declaration on top of an HTML document didn't automatically transform it into an XHTML document , although admittedly that was what everybody was hoping for.实际发生的事情是人们没有意识到仅仅将 XHTML doctype 声明放在 HTML 文档之上并不会自动将其转换为 XHTML 文档,尽管无可否认这是每个人都希望的。

You see, most web applications out there aren't configured to serialize XHTML documents as application/xhtml+xml properly, instead opting to serve pages as just text/html .您会看到,大多数 Web 应用程序都没有配置为将 XHTML 文档正确地序列化为application/xhtml+xml ,而是选择将页面仅作为text/html (It's typically because of the .html file extension more than anything else, really; generally speaking, servers do correctly apply application/xhtml+xml to documents with .xhtml or .xht as the extension, but only static sites that actually make use of the file format will benefit from this.) That leads browsers to decide that they received a regular HTML document, and so that tag soup parsing nonsense we've all come to know and love inevitably ensues. (这通常是因为.html文件扩展名比其他任何事情都重要;一般来说,服务器确实正确地将application/xhtml+xml应用于以.xhtml.xht作为扩展名的文档,但只有实际使用的静态站点文件格式将从中受益。)这导致浏览器决定他们收到一个常规的 HTML 文档,因此标签汤解析废话我们都已经知道并且不可避免地随之而来。

Note that it doesn't matter even if you have a meta tag like this on your XHTML document:请注意,即使您的 XHTML 文档中有这样的元标记也没关系:

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

Browsers will ignore that, and only look at the actual HTTP Content-Type header that was sent along with the XHTML document.浏览器将忽略它,而只查看与 XHTML 文档一起发送的实际 HTTP Content-Type标头。

To make matters worse, Internet Explorer, being the most-used browser in the past few years in XHTML's heyday, never properly supported the application/xhtml+xml MIME type before version 9 was finally released: instead of parsing the markup, constructing the DOM and rendering the page, all it would do was ask for a file download.更糟糕的是,Internet Explorer 作为 XHTML 鼎盛时期过去几年中使用最多的浏览器,在版本 9 最终发布之前从未正确支持application/xhtml+xml MIME 类型:不是解析标记,而是构建 DOM并呈现页面,它所做的只是要求下载文件。 That doesn't make a very usable XHTML page!这不会使 XHTML 页面非常有用!

So, guess what we all had to live with until HTML5 became cool?那么,猜猜在 HTML5 变得很酷之前,我们都必须忍受什么?

This, along with things like IE6 going quirky on pages with the XML declaration before the doctype declaration, is also one of the biggest factors leading to XHTML's downfall (along with XHTML 1.1 never gaining widespread usage, and XHTML 2.0 being canceled in favor of HTML5).这一点,以及像 IE6 在 doctype 声明之前在 XML 声明的页面上变得古怪的事情一样,也是导致 XHTML 垮台的最大因素之一(以及 XHTML 1.1 从未获得广泛使用,以及 XHTML 2.0 被取消以支持 HTML5 )。

Most people use the XHTML doctype because they read it in an old book somewhere or read it on a forum but otherwise are using it for no technical reason they are aware of.大多数人使用 XHTML 文档类型是因为他们在某处的一本旧书中或在论坛上阅读过它,但除此之外,他们没有意识到技术原因而使用它。 Hardly anyone uses it properly by serving it as application/xml+xhtml.几乎没有人通过将其作为 application/xml+xhtml 来正确使用它。 Serving XHTML pages as text/html means "tag soup" or "broken html".以 text/html 形式提供 XHTML 页面意味着“标记汤”或“损坏的 html”。 It should not be done but browsers generally handle it well.不应该这样做,但浏览器通常可以很好地处理它。

You are correct in your wondering about this.你对此的疑惑是正确的。 It drives me crazy.它让我疯狂。

I assume that you're asking why people are serving XHTML documents as HTML, by using the text/html MIME type instead of application/xhtml+xml .我假设您在问为什么人们通过使用text/html MIME 类型而不是application/xhtml+xml来提供 XHTML 文档作为 HTML。

Mostly, it's because of a misguided understanding of compatibility: Lots of browsers simply don't understand the XHTML+XML MIME type, which has caused users to simply serve it as HTML to overcome this.大多数情况下,这是因为对兼容性的错误理解:许多浏览器根本不理解 XHTML+XML MIME 类型,这导致用户简单地将其作为 HTML 提供以克服这一点。 Since browsers often don't complain about what they get, and people don't tend to research a lot, most people assume that the browsers just treat the XHTML-doctyped document as XHTML, even though it was served as HTML.由于浏览器通常不会抱怨他们得到了什么,而且人们也不倾向于进行大量研究,因此大多数人认为浏览器只是将 XHTML 文档类型的文档视为 XHTML,即使它是作为 HTML 提供的。 But they don't - thry serve them as HTML.但他们没有 - 尝试将它们作为 HTML 提供。 Since the two languages are so much alike, people rarely notice the difference.由于这两种语言非常相似,人们很少注意到差异。

So no, you're not missing anything;所以不,你没有错过任何东西; it's very bad practice.这是非常糟糕的做法。 Nowadays, after HTML5, luckily, it seems to become less common.如今,幸运的是,在 HTML5 之后,它似乎变得不那么普遍了。

The hilarious thing about XHTML is that because IE didn't understand the XML mimetype ( application/xhtml+xml ) at the peak of XHTML's popularity, most people never actually used the XML part of it as IE8 and lower refuse to render the content. XHTML 的可笑之处在于,由于 IE 不了解 XHTML 流行高峰期的 XML mimetype ( application/xhtml+xml ),因此大多数人从未真正将其 XML 部分用作 IE8 并且更低的拒绝呈现内容。

This meant that millions of sites think they are using standards compliant XHTML, when in fact they are being parsed as malformed/weird HTML4.这意味着数以百万计的站点认为他们正在使用符合标准的 XHTML,而实际上它们被解析为格式错误/奇怪的 HTML4。

Luckily HTML5 came along and properly defined the parsing of documents, removing much of the ambiguity that surrounded XHTML (all that transitional and strict rubbish).幸运的是,HTML5 出现并正确定义了文档的解析,消除了围绕 XHTML 的大部分歧义(所有这些过渡性和严格的垃圾)。

People who add the XML prolog before the doctype are doing themselves an extra disservice, as a comment before the doctype will cause old IE to use quirks mode, which among other things brings back the old box-model in IE6 and below.在 doctype 之前添加 XML prolog 的人对自己造成了额外的伤害,因为在 doctype 之前的注释会导致旧的 IE 使用 quirks 模式,其中包括在 IE6 及更低版本中带回旧的框模型。 This undoubtedly has contributed to the mass hate of IE6, as in quirks mode it has significant bugs that cause modern layouts to be completely broken, rather than just lacking in newer features.这无疑导致了 IE6 的大众仇恨,因为在 quirks 模式下,它具有导致现代布局完全破坏的重大错误,而不仅仅是缺乏更新的功能。

The short answer is that in this industry many people just copy and paste code without understanding it.简短的回答是,在这个行业中,很多人只是复制和粘贴代码而不理解它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM