为什么要在HTML文件上放置一个XHTML文档类型声明?这是做什么的? [英] Why put an XHTML doctype declaration on HTML files? What does that do?

查看:102
本文介绍了为什么要在HTML文件上放置一个XHTML文档类型声明?这是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我遇到的网页数量是否是HTML文件,但是会带上XHTML DOCTYPE声明。

为什么人们这样做?他们希望实现什么?为什么不保留实际XHTML文件的XHTML doctype声明?



或者我错过了什么?



编辑:关于什么是实际的XHTML文件存在一些混淆;为了证明差异不是由DOCTYPE声明造成的,请将此文件这一个。第一个是HTML,第二个是XHTML,虽然内容是相同的;只有文件类型不同。两者在兼容的浏览器中都显示正常,但第一个用HTML解析器解析,第二个用XML解析器解析。

解决方案


为什么要在HTML文件上放置一个XHTML文档类型声明?这是干什么的?


所有这些都会告诉标记验证程序他们即将验证XHTML文档,而不是常规的,基于SGML的HTML文档。它描述了内容,或者更具体地说是后面的标记,但没有其他描述。


人们为什么这样做?他们希望实现什么?为什么不保留实际的XHTML文件的XHTML文档类型声明?



或者我错过了什么?


种。实际上发生的事情是人们并不知道只是在HTML文档的顶部放置一个XHTML文档类型声明并不会自动将其转换为XHTML文档 ,尽管无可否认这是每个人都希望的。

你看,大多数Web服务器和/或Web应用程序没有配置为将XHTML文档序列化为 application / xhtml + xml ,而是选择只为 text / html 提供网页。 (对于服务器来说,通常是因为 .html 文件扩展名比其他任何东西都要多)。这会导致浏览器决定他们收到一个普通的HTML文档,所以标签汤解析胡说八道,我们都知道爱是不可避免的。



请注意,即使您在您的XHTML上有这样的meta标签也没关系文档:

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

浏览器会忽略它,只查看实际的HTTP Content-Type

标题与XHTML文档一起发送。



更糟糕的是,Internet Explorer是过去最常用的浏览器在XHTML的鼎盛时期,从未正确地支持版本9最终发布之前的 application / xhtml + xml MIME类型:不是解析标记,构建DOM并渲染页面,它所要做的只是要求下载文件。这不会构成一个非常有用的XHTML页面!



所以,想一想我们都必须忍受什么,直到HTML5变凉快为止。



这和IE6一样,在带有XML声明的页面上古怪在doctype声明之前,它也是导致XHTML垮台的最大因素之一(与XHTML 1.1从未广泛使用,并且XHTML 2.0被取消而支持HTML5)。

I wonder about the number of web pages I encounter that are HTML files, but that wear an XHTML DOCTYPE declaration.
Why are people doing this? What do they hope to achieve? Why not reserve the XHTML doctype declaration for actual XHTML files?

Or am I missing something?

Edit: there is some confusion about what "actual XHTML files" are; to demonstrate that the difference is not caused by the DOCTYPE declaration, compare this file to this one. The first is HTML, the second is XHTML, although the contents are identical; 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.

解决方案

Why put an XHTML doctype declaration on HTML files? 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. 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?

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.

You see, most Web servers and/or Web applications out there aren't configured to serialize XHTML documents as application/xhtml+xml properly, instead opting to serve Web pages as just text/html. (For servers, it's typically because of the .html file extension more than anything else, really.) 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.

Note that it doesn't matter even if you have a meta tag like this on your XHTML document:

<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.

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. That doesn't make a very usable XHTML page!

So, guess what we all had to live with until HTML5 became cool?

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).

这篇关于为什么要在HTML文件上放置一个XHTML文档类型声明?这是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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