Html HTML5 是有效的 XML 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5558502/
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
Is HTML5 valid XML?
提问by Hyman B.
I am confused. A co-worker turned me on to the possibility that tags ending in />
, such as <br />
can still be used in HTML5. I thought that only <br>
-style could be used. All of the "talk" across the Internet is about using the latter.
我很迷惑。一位同事让我意识到以/>
,结尾的标签<br />
仍然可以在 HTML5 中使用的可能性。我认为只能使用<br>
-style 。互联网上的所有“讨论”都是关于使用后者。
Could someone please explain this to me? This seems very confusing and poorly documented.
有人可以向我解释一下吗?这似乎非常令人困惑且记录不全。
And this brings up another question: Is HTML 5 considered to be well-formed XML?
这就引出了另一个问题:HTML 5 是否被认为是格式良好的 XML?
回答by Matthew Flaschen
There is an XML serialization of it, called XHTML5. Basically, you're free to use either HTML5 (HTML serialization) or XHTML5 (XML serialization). The draft spec says HTML5 "is the format suggested for most authors," mainly for the same reasons people recommend text/html
for XHTML 1.1.
它有一个 XML 序列化,称为XHTML5。基本上,您可以自由使用 HTML5(HTML 序列化)或 XHTML5(XML 序列化)。规范草案说 HTML5“是为大多数作者推荐的格式”,主要是出于人们推荐text/html
XHTML 1.1的相同原因。
回答by unor
You can markup your page as valid HTML5 andXHTML5: http://www.w3.org/TR/html-polyglot/
您可以将您的页面标记为有效的 HTML5和XHTML5:http: //www.w3.org/TR/html-polyglot/
Polyglot markup that meets a well defined set of constraints is interpreted as compatible, regardless of whether they are processed as HTML or as XHTML, per the HTML5 specification.
根据 HTML5 规范,符合一组明确定义的约束的多语言标记被解释为兼容,无论它们是作为 HTML 还是 XHTML 处理。
The basic document could look like:
基本文件可能如下所示:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title></title>
</head>
<body>
</body>
</html>
Of course you'd have to follow some additional rules (like not to use the noscript
element, for example), outlined in the linked working draft.
当然,您必须遵循noscript
链接的工作草案中概述的一些附加规则(例如不使用该元素)。
回答by kevinji
HTML5 can be written with or without self-closing slashes; it is meant to be backwards-compatible with both HTML 4.01 and XHTML 1.0 code, so that it is easy to convert code into valid HTML5. There isan XML serialization called XHTML5, but for backwards-compatibility purposes with IE browsers, it is not recommended to be used. So technically, HTML5 is notconsidered to be well-formed XML.
HTML5 可以写有或没有自闭斜杠;它旨在向后兼容 HTML 4.01 和 XHTML 1.0 代码,因此很容易将代码转换为有效的 HTML5。这里是被称为XHTML5的XML序列化,但为了向后兼容的目的与IE浏览器,所以不建议使用。所以从技术上讲,HTML5不被认为是格式良好的 XML。
回答by NotMe
Under no condition should you expect any html document (regardless of version) to be "well-formed xml"
在任何情况下,您都不应期望任何 html 文档(无论版本如何)都是“格式良好的xml”
html != xml.
html != xml。
It is a different spec with different suggestions (I'm purposely avoiding the word "rules" here) on how it should be interpreted.
这是一个不同的规范,有不同的建议(我在这里故意避免使用“规则”这个词)关于它应该如何解释。
The HTML 5 spec has enough "do it this way, but it's okay if you don't" wiggle statements that it's a wonder that any of the browsers show the same thing at all.
HTML 5 规范中有足够多的“这样做,但如果你不这样做也没关系”的声明,即任何浏览器都显示相同的内容是一个奇迹。