HTML 5 与 XHTML 1.0 过渡?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/256953/
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:39:03  来源:igfitidea点击:

HTML 5 versus XHTML 1.0 Transitional?

xhtmlhtml

提问by Brennan

It seems that HTML 5 is going to be supported (partially) by Firefox 3.1 and other browsers. It is adding support for video and audio as tags, but these are new tags that XHTML 1.0 Transitional does not recognize. What is the behavior supposed to be if I use a new HTML 5 tag in a future version of Firefox but use the DTD for XHTML? And what if I mix HTML 5 markup with XHTML 1.0 Trans?

Firefox 3.1 和其他浏览器似乎将(部分)支持 HTML 5。它增加了对视频和音频作为标签的支持,但这些是 XHTML 1.0 Transitional 无法识别的新标签。如果我在未来版本的 Firefox 中使用新的 HTML 5 标签,但对 XHTML 使用 DTD,应该会出现什么行为?如果我将 HTML 5 标记与 XHTML 1.0 Trans 混合会怎样?

This is getting confusing. Why didn't they just add these tags to XHTML? How do we support both XHTML and HTML 5?

这越来越令人困惑。他们为什么不直接将这些标签添加到 XHTML 中?我们如何同时支持 XHTML 和 HTML 5?

Video on HTML 5: http://www.youtube.com/watch?v=xIxDJof7xxQ

HTML 5 视频:http: //www.youtube.com/watch?v=xIxDJof7xxQ

采纳答案by hangy

Well, generally speaking HTML is SGML and XHTML is expressed in XML. Because of that, creating XHTML is connected with more restrictions (in the form of markup) than HTML is. (SGML-based versus XML-based HTML)

嗯,一般来说 HTML 是 SGML,XHTML 是用 XML 表示的。因此,与 HTML 相比,创建 XHTML 会受到更多限制(以标记的形式)。(基于 SGML 与基于 XML 的 HTML

As mentioned on Wikipedia, HTML 5 will also have a XHTML variant (XHTML 5).

正如在Wikipedia 上提到的,HTML 5 也将有一个 XHTML 变体 (XHTML 5)。

Rule of thumb: You should always use valid markup. That also means that you should not use the mentioned <video>or <audio>tags in XHTML 1.0 Transitional, as those are not an element of that specification. If you really needto use those tags (which I highly doubt), then you should make sure that you use the HTML 5/XHTML 5 DTD in order to specify that your document is in that DOCTYPE.

经验法则:您应该始终使用有效的标记。这也意味着您不应使用XHTML 1.0 Transitional 中提到的<video><audio>标签,因为它们不是该规范的元素。如果您真的需要使用这些标签(我非常怀疑),那么您应该确保使用 HTML 5/XHTML 5 DTD 以指定您的文档在该 DOCTYPE 中。

Using HTML 5 or XHTML 5 in the given state of the implementation (AFAIK, the standard is not even settled, yet, correct?) could be counter-productive, as almost all users may not see the website rendered correclty anyways.

在给定的实现状态下使用 HTML 5 或 XHTML 5(AFAIK,标准甚至还没有确定,但是,对吗?)可能会适得其反,因为几乎所有用户都可能看不到网站呈现的正确性。

Edit 2013: Because of the recent downvotes and since this accepted answer cannot be deleted (by me), I would like to add that the support and standardization process of HTML5 is nowadays totally different to what it was when I wrote this answer five years ago. Since most major browsers support most parts of the HTML5 draft and because a lot of stuff can be fixed with polyfills in older browsers, I mainly use HTML5 now.

2013 年编辑:由于最近的反对票以及此已接受的答案无法删除(由我),我想补充一点,现在 HTML5 的支持和标准化过程与我五年前编写此答案时的完全不同. 由于大多数主流浏览器都支持 HTML5 草案的大部分内容,并且因为在旧浏览器中可以使用 polyfills 修复很多东西,所以我现在主要使用 HTML5。

回答by Shadow2531

HTML5 is so much easier to write than XHTML 1.0.

HTML5 比 XHTML 1.0 更容易编写。

  1. You don't have to manually declare the "http://www.w3.org/1999/xhtml" namespace.

  2. You don't have to add type attributes to script and style elements (they default to text/javascript and text/css).

  3. You don't have to use a long doctype where the browser just ignores most of it. You must use <!DOCTYPE html>, which is easy to remember.

  4. You don't have a choice to include or not include a dtd uri in the doctype and you don't have a choice between transitional and strict. You just have a strict doctype that invokes full standards mode. That way, you don't have to worry about accidentally being in Almost standards mode or Quirks mode.

  5. The charset declaration is much simpler. It's just <meta charset="utf-8">.

  6. If you find it confusing to write void elements as <name>, you can use <name/>, if you want.

  7. HTML5 has a really good validator at http://validator.nu/. The validator isn't bound by a crappy DTD that can't express all the rules.

  8. You don't have to add //<![CDATA etc. in inline scripts or stylesheets (in certain situations) to validate.

  9. You can use embed if needed.

  1. 您不必手动声明“ http://www.w3.org/1999/xhtml”命名空间。

  2. 您不必为脚本和样式元素添加类型属性(它们默认为 text/javascript 和 text/css)。

  3. 您不必使用很长的文档类型,浏览器会忽略其中的大部分内容。您必须使用 <!DOCTYPE html>,它很容易记住。

  4. 您无法选择在文档类型中包含或不包含 dtd uri,并且您无法在过渡和严格之间进行选择。您只有一个调用完整标准模式的严格文档类型。这样,您就不必担心意外处于“几乎标准”模式或“怪癖”模式。

  5. 字符集声明要简单得多。它只是 <meta charset="utf-8">。

  6. 如果您发现将 void 元素编写为 <name> 会令人困惑,您可以根据需要使用 <name/>。

  7. HTML5 在http://validator.nu/ 上有一个非常好的验证器。验证器不受无法表达所有规则的蹩脚 DTD 的约束。

  8. 您不必在内联脚本或样式表(在某些情况下)中添加 //<![CDATA 等来验证。

  9. 如果需要,您可以使用嵌入。

Just syntax-wise, when you use HTML5, you end up with cleaner, easier to read markup that always invokes standards mode. When you use XHTML 1.0 (served as text/html), you're specifying a bunch of crud (in order to validate against a crappy dtd) that the browser will do automatically.

就语法而言,当您使用 HTML5 时,您最终会得到更清晰、更易于阅读的标记,这些标记总是调用标准模式。当您使用 XHTML 1.0(用作 text/html)时,您指定了一堆浏览器将自动执行的 crud(为了针对蹩脚的 dtd 进行验证)。

回答by Brennan

Myths and misconceptions abound in this thread.

神话和误解在这个线程中比比皆是。

  1. XHTML 1.0 is older than HTML 5. It cannot use any new vocabulary. Indeed, its main selling point was that it uses exactly the same vocabulary as HTML 4.01.

  2. There will be no XHTML 1.2 - most probably. And it is not needed. XHTML 5 is the XML serialization of HTML 5. Identical vocabulary, different parsing rules.

  3. HTML has never been treated as true SGML in browsers. No browser has ever implemented an SGML-compliant parser. HTML 5 will make this fact into a rule and the HTML serialization will follow todays de facto standard. One could perhaps say that it is "SGML-ish".

  4. As it has been stated, the DTD serves exactly one purpose IN BROWSERS, and that is to distinguish between standards compliance mode and quirks mode. Thus it affects only styling and scripting. If you are using frames on a page with astrict doctype, they will render just fine. As will <embed> and even <marquee> - even though the latter is an abomination and the former not in any current standard. It is part of HTML 5, though.

  5. Video and audio can be used regardless of serialization, XML or HTML. they are part of both HTML 5 and XHTML 5. Once the parsing stage is over a browser will have constructed an internal DOM of the document. That DOM will be for all practical purposes the same regardless of serialization. And yes, XHTML sent with text/html is still normal html, regardless of doctype.

  1. XHTML 1.0 比 HTML 5 旧。它不能使用任何新词汇。事实上,它的主要卖点是它使用与 HTML 4.01 完全相同的词汇。

  2. 不会有 XHTML 1.2 - 很有可能。它不是必需的。XHTML 5 是 HTML 5 的 XML 序列化。相同的词汇,不同的解析规则。

  3. HTML 在浏览器中从未被视为真正的 SGML。没有浏览器实现过符合 SGML 的解析器。HTML 5 将这一事实变成规则,并且 HTML 序列化将遵循今天的事实标准。人们或许可以说它是“SGML-ish”。

  4. 如前所述,DTD 在浏览器中只服务于一个目的,即区分标准遵从模式和怪癖模式。因此它只影响样式和脚本。如果您在具有严格文档类型的页面上使用框架,它们将呈现得很好。就像 <embed> 甚至 <marquee> 一样 - 即使后者令人憎恶,而前者不符合任何现行标准。不过,它是 HTML 5 的一部分。

  5. 无论序列化、XML 还是 HTML,都可以使用视频和音频。它们是 HTML 5 和 XHTML 5 的一部分。一旦解析阶段结束,浏览器将构建文档的内部 DOM。无论序列化如何,DOM 对于所有实际目的都是相同的。是的,无论文档类型如何,使用 text/html 发送的 XHTML 仍然是普通的 html。

回答by soypunk

You might be looking at the problem the wrong way because the relationship to XHTML 1.xsection, HTML 5 states:

您可能以错误的方式看待问题,因为与XHTML 1.x部分的关系,HTML 5 指出:

"This specification is intended to replace XHTML 1.0 as the normative definition of the XML serialization of the HTML vocabulary."

“该规范旨在取代 XHTML 1.0 作为 HTML 词汇表的 XML 序列化的规范定义。”

Now that language is controversial (the XHTML 2 WG has disputed it and the HTML WG is trying to resolve the differences...) but that's where we stand right now.

现在该语言是有争议的(XHTML 2 WG 对它有争议,HTML WG 正在努力解决差异......)但这就是我们现在的立场。

A couple of notes:

一些注意事项:

回答by Ms2ger

Keep in mind that doctypes only serve one purpose in browsers: switch between quirks, almost standards and standards mode. Therefore, using <video>and <audio>will work with any doctype declaration. IMO, using an XHTML doctype is quite useless, as every page you send with text/htmlMIME type is parsed as (tag-soup) HTML anyways. I suggest using the HTML5 doctype (<!doctype html>), as it is easier to remember and doesn't force you in XML syntax without a reason.

请记住,文档类型在浏览器中仅用于一个目的:在怪癖、几乎标准和标准模式之间切换。因此,使用<video><audio>将适用于任何文档类型声明。IMO,使用 XHTML 文档类型是非常无用的,因为您发送的每个带有text/htmlMIME 类型的页面都被解析为(标签汤)HTML。我建议使用 HTML5 doctype ( <!doctype html>),因为它更容易记住并且不会无缘无故地强迫您使用 XML 语法。

Why didn't they just add these tags to XHTML?

他们为什么不直接将这些标签添加到 XHTML 中?

They actually did, there is an XML serialization of HTML 5 (XHTML5). To use this, you have to send your pages with an XML MIME type, such as application/xhtml+xml. This is not (yet) supported by IE, though.

他们确实做到了,有一个 HTML 5 (XHTML5) 的 XML 序列化。要使用它,您必须发送带有 XML MIME 类型的页面,例如application/xhtml+xml. 但是,IE(尚)不支持此功能。

回答by codeinthehole

What is the behavior supposed to be if I use a new HTML 5 tag in a future version of Firefox but use the DTD for XHTML?

And what if I mix HTML 5 markup with XHTML 1.0 Trans?

如果我在未来版本的 Firefox 中使用新的 HTML 5 标签,但对 XHTML 使用 DTD,应该会出现什么行为?

如果我将 HTML 5 标记与 XHTML 1.0 Trans 混合会怎样?

If your markup isn't implemented as part of your chosen DTD - then logically, that markup shouldn't be followed. But browser implementations aren't always strictly logical.

如果您的标记不是作为您选择的 DTD 的一部分实现的 - 那么从逻辑上讲,不应遵循该标记。但是浏览器的实现并不总是严格合乎逻辑的。

Why didn't they just add these tags to XHTML? How do we support both XHTML and HTML 5?

他们为什么不直接将这些标签添加到 XHTML 中?我们如何同时支持 XHTML 和 HTML 5?

xHTML is not better than HTML, but it's more suited to some applications. One of the main benefits of xHTML is that it can be transformed into different formats using XSLT. For example, you could use XSLT to automatically transform xHTML into an RSS feed or another XML format.

xHTML 并不比 HTML 好,但它更适合某些应用程序。xHTML 的主要优点之一是可以使用 XSLT 将其转换为不同的格式。例如,您可以使用 XSLT 自动将 xHTML 转换为 RSS 提要或其他 XML 格式。

You don't need to support both formats - weigh up the benefits/drawbacks for each with your project's requirements. HTML 5 probably won't be standard for quite some time.

您不需要同时支持这两种格式 - 根据您的项目要求权衡每种格式的优点/缺点。HTML 5 可能在很长一段时间内都不会成为标准。

回答by singpolyma

(X)HTML5 is just the next version. You should be using XHTML1.1 until XHTML5 is well-supported.

(X)HTML5 只是下一个版本。在 XHTML5 得到很好的支持之前,您应该使用 XHTML1.1。

You probably should not use the backwards-compatability SGML profile of HTML5. It makes things harder for scrapers and small parsers.

您可能不应该使用 HTML5 的向后兼容性 SGML 配置文件。它使刮板和小型解析器变得更加困难。

回答by Greg

Your doctypewill tell the browser whether you're using HTML5 or XHTML. You can't just shove a tag from one doctype into a document of another doctype and expect it to work. Without a doctype, it's all just tag soup anyway.

您的文档类型会告诉浏览器您使用的是 HTML5 还是 XHTML。您不能只是将一个 doctype 中的标签推到另一个 doctype 的文档中并期望它起作用。没有文档类型,无论如何都只是标签汤。

回答by Andrew G. Johnson

Don't use things like video/audio tags when 99% of people won't be able to view it properly on their browser. For either of these two examples I'd suggest using FLV.

当 99% 的人无法在他们的浏览器上正确查看时,不要使用视频/音频标签之类的东西。对于这两个示例中的任何一个,我建议使用 FLV。

As far as why they don't add it to XHTML... firstly 1.0 isn't the most recent version, 1.1 was released a while ago.

至于他们为什么不将它添加到 XHTML...首先 1.0 不是最新版本,1.1 是在不久前发布的。

Eventually things get standardized and we'll see these types of tags in both standards, but for now just do what you can to ensure the most amount of people can view your content.

最终事情会变得标准化,我们将在两个标准中看到这些类型的标签,但现在尽你所能确保最多的人可以查看您的内容。