对xhtml5感到困惑:不再有`<?xml?>`和现在强制的“元”? [英] confused about xhtml5: no more `<?xml?>` and now mandatory `meta`?

查看:151
本文介绍了对xhtml5感到困惑:不再有`<?xml?>`和现在强制的“元”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直是 XHTML 1.0 Strict 的长期使用者,现在我正在尝试在我的新项目中切换到XHTML5。

I've been a longtime user of XHTML 1.0 Strict, and I'm now trying to switch to XHTML5 in my new projects.

我很困惑,<?xml version ='1.0'encoding ='utf-8'?> 不再被视为有效HTML5版, http://validator.w3.org/ 。这是为什么?是不是所有的xml文件应该开始?

I'm confused that <?xml version='1.0' encoding='utf-8'?> is no longer considered valid, for HTML5, by http://validator.w3.org/. Why is that? Isn't that what all xml documents are supposed to start with?

当我删除标准的<?xml …我的文档仍然无法验证:现在它缺少编码。我不喜欢那些 meta 标签,但现在他们现在是强制性的,要指定编码,以便有效(X)HTML5?

And when I remove the standard <?xml…, my document still doesn't validate: now it's missing the encoding. I don't like those meta tags, but are they now effectively mandatory, to specify the encoding, in order to be valid (X)HTML5?

推荐答案

XML声明在HTML5的XHTML序列化中有效并验证。以下相当简单的文档验证:

An XML declaration is valid and validates in XHTML serialization of HTML5. The following rather minimal document validates:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body></body>
</html>

但是,这仅适用于HTML5的XHTML序列化(XHTML语法)。在HTML序列化中,不允许。如果您将上述文档写入文件并将其存储在将以 Content-Type:text / html 发送的服务器(通常会在文件名以 .html),那么您会收到一条错误消息:

However, this only applies to XHTML serialization (XHTML syntax) of HTML5. In HTML serialization, it is not allowed. If you write the above document in a file and store it in a server that will send it with Content-Type: text/html (which normally happens if the filename ends with ".html"), then you get an error message:

Saw <?. Probable cause: Attempt to use an XML processing instruction in HTML.
(XML processing instructions are not supported in HTML.)

这里HTML表示HTML

Here "HTML" means HTML serialization only.

浏览器不关心任何语法中的XML声明。在HTML语法中,它被忽略,作为可恢复的语法错误。在XHTML语法中,没有关系,除了编码部分。

Browsers do not care about an XML declaration in either syntax. In HTML syntax, it is just ignored, as a recoverable syntax error. In XHTML syntax, it does not matter, except for the encoding part.

尽管XML 1.0规范建议(但是不需要) XML声明,实际上(除了 encoding 的重要性)仅适用于能够处理不同版本的XML的软件。浏览器不是。除了XML 1.0之外,还有只有XML 1.1,没有太多用处。此外,定义了HTML5,使得XHTML语法中使用的XML版本是XML 1.0。

Although XML 1.0 specification recommends (but does not require) an XML declaration, it would in practice matter (apart from the significance of encoding) only to software that is capable of processing different versions of XML. Browsers aren’t. And in addition to XML 1.0, there’s just XML 1.1, which is not used much. Besides, HTML5 is defined so that the XML version used in XHTML syntax is XML 1.0.

encoding part但是,对于XML,默认情况是 utf-8 。如果由于某些原因使用其他编码,则XML声明可能有助于防止任何冲突。 HTML5 CR在其中表示编码的讨论:在XHTML中,如果需要,应该使用XML声明来进行内联字符编码信息。当使用XML内容类型服务时,一个标签在XHTML中无法真正帮助,因为编码具有已经被确定(默认为UTF-8或其他方式)。

The encoding part may matter, but utf-8 is the default anyway for XML. If you use another encoding for some reason, then an XML declaration may be useful to prevent any conflicts. HTML5 CR says this in it discussion of encodings: "In XHTML, the XML declaration should be used for inline character encoding information, if necessary." A meta tag cannot really help in XHTML when served with an XML content type, since the encoding has already been decided (by defaulting to UTF-8 or otherwise) when the tag is seen.

对于HTML语法,< meta charset = ...> 标签可能可以使用,但不需要有效性,编码可以在HTTP标头中指定(覆盖任何 meta 标签)。但是,使用 meta 标签可能会有所帮助,因为页面可能在本地保存,然后打开时不会有任何HTTP标头可用。

For HTML syntax, the <meta charset=...> tag may be used, but it is not needed for validity, and the encoding can be specified in HTTP headers (which override any meta tags). Using a meta tag may however be helpful, since a page might be saved locally, and then there won’t be any HTTP headers available when it is opened.

这篇关于对xhtml5感到困惑:不再有`&lt;?xml?&gt;`和现在强制的“元”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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