XHTML和HTML之间的主要区别是什么?

时间:2022-10-27 20:06:27

What are the main differences between XHTML and HTML? Which one is better in your opinion, and why? Do most browsers support both?

XHTML和HTML之间的主要区别是什么?你认为哪个更好,为什么?大多数浏览器都支持这两种功能吗?

8 个解决方案

#1


15  

This is probably the best article I've read on the differences and relative merits of each:

这可能是我读过的最好的一篇关于每一种方法的不同和相对优点的文章:

HTML Versus XHTML

Which should we use, HTML or XHTML, and why?

我们应该使用HTML还是XHTML,为什么呢?

There is also a rather technical comparison on the WHATWG wiki.

在WHATWG wiki上还有一个相当技术性的比较。

#2


14  

XHTML is based on XML, and thus requires the source to be well-formed. Since XHTML is more strict than HTML, less pre-processing is needed by the rendering engine.

XHTML基于XML,因此要求源代码格式良好。由于XHTML比HTML更严格,因此呈现引擎需要的预处理更少。

XHTML should be served as application/xhtml+xml for you to take advantage of the benefits, otherwise XHTML will be treated as ordinary HTML. Serving it as 'application/xhtml+xml' is not common on the web due to Internet Explorer, which cannot handle XHTML.

XHTML应该作为应用程序/ XHTML +xml来使用,否则XHTML将被视为普通的HTML。由于Internet Explorer不能处理xhtml,因此在web上作为“应用程序/xhtml+xml”服务并不常见。

#3


9  

The difference is that XHTML is based on XML while HTML is based on SGML.

不同之处在于XHTML是基于XML的,而HTML是基于SGML的。

Browsers use the SGML parser for content sent with the content type text/html and the XML parser for application/xhtml+xml.

浏览器使用SGML解析器处理与内容类型文本/html一起发送的内容,使用XML解析器处理应用程序/xhtml+ XML。

When using the SGML parser, browsers will continue the parsing even when they encounter a syntax error in the file. This is why so many people think they are doing XHTML when they are sending XML-looking files to the browser. This is actually a mistake since this will trigger many parsing errors in the browser and slow the rendering process.

当使用SGML解析器时,浏览器将继续解析,即使在文件中遇到语法错误。这就是为什么很多人在向浏览器发送xml格式的文件时认为他们在做XHTML。这实际上是一个错误,因为这将在浏览器中触发许多解析错误并减慢呈现过程。

When using the XML parser, browser will stop the parsing when a syntax error is encountered and display an XML error. This is of course only true for browsers that have an XML parser for HTML content, which is not true for Internet Explorer that will only download the file and not display it.

当使用XML解析器时,当遇到语法错误时,浏览器将停止解析并显示XML错误。当然,这只适用于具有HTML内容XML解析器的浏览器,而不适用于只下载文件而不显示文件的Internet Explorer。

What should you do then?

那你该怎么办呢?

You should use XHTML if you need the syntax checking and the strict structure that XML impose. You should keep in mind that you must be sending your XHTML content to Internet Explorer with the wrong content type with all the issues implied. You should also keep in mind that your document will break whenever an unvalid content is in the file, so you must take great care to sanitize any user input.

如果需要语法检查和XML强加的严格结构,则应该使用XHTML。您应该记住,您必须以错误的内容类型将XHTML内容发送到Internet Explorer,其中包含所有的问题。您还应该记住,当文件中有无效内容时,您的文档将会中断,因此您必须非常小心地处理任何用户输入。

You should use HTML for anything else. HTML just works and is a greatly supported standard today. Even the next standard HTML 5 defines an SGML-based syntax so this will last.

您应该使用HTML进行其他操作。HTML仅仅是有效的,并且是当今非常受支持的标准。甚至下一个标准的HTML 5也定义了一个基于sgml的语法,因此这种语法将持续下去。

Keep in mind that whatever the format you choose, validating your output is always a good idea to detect syntax error.

请记住,无论选择何种格式,验证输出始终是检测语法错误的好方法。

#4


8  

The Extensible Hypertext Markup Language, or XHTML, is a markup language that has the same depth of expression as HTML, but also conforms to XML syntax.

可扩展超文本标记语言(或XHTML)是一种标记语言,具有与HTML相同的表达深度,但也符合XML语法。

XHTML is "the modern version of HTML 4".

XHTML是“HTML 4的现代版本”。

More info : wikipedia and W3C

更多信息:*和W3C

#5


3  

As previously stated, XHTML should be (in theory) valid XML. In theory (but not in practice) non-conforming XHTML should not be rendered by the browser.

如前所述,XHTML(理论上)应该是有效的XML。在理论上(但不是在实践中),不符合的XHTML不应该由浏览器呈现。

There is very rarely an advantage to use one over the other, so write whichever you personally prefer - as long as you do it consistently. HTML5 which is going to be the future web-standard will support both XHTML and HTML 4.01-style tags (the prior via optional "XML serialization"), and XHTML2 is looking like it will be pretty much dead in the water with no vendors actively supporting it.

使用一种方法比使用另一种方法几乎没有什么好处,所以你可以用你自己喜欢的方法来写作——只要你坚持这样做。HTML5将成为未来的web标准,它将支持XHTML和HTML 4.01样式的标签(之前通过可选的“XML串行化”),而XHTML2看起来将会在没有供应商积极支持它的情况下死水一潭。

#6


2  

Summary: use HTML.

摘要:使用HTML。

There are many differences, and many smart people (that know what they are talking about :D) have already summed up most of the pros and cons.

有很多不同之处,很多聪明人(他们知道自己在说什么:D)已经总结出了大多数的利弊。

#7


1  

It's not about which one is better; the HTML still continues to develop, and HTML5 is on its way (for years, though :) ) and delivers some new elements for easier management of new technologies like multimedia in web pages.

不是哪个更好;HTML仍在继续开发,HTML5也在发展中(尽管已经有很多年了),并交付了一些新元素,以便在web页面中更容易地管理多媒体等新技术。

On the other hand, XHTML is about strictness of XML, should we say keeping things clean. If you keep your HTML document well-formed (close every opened element, keep things nested in the tree-form), you're getting the best out of XHTML/XML world and can still be using the HTML format, declaring your documents to be HTML, but still keeping them 'clean' (well-formed). I don't think we should declare each of them 'better'; they can co-exist. It's just about us doing our things the right way.

另一方面,XHTML是关于XML的严格性的,我们应该说保持事物的整洁。如果您保持HTML文档格式良好(关闭每个打开的元素,将其嵌套在树状中),那么您将从XHTML/XML世界中得到最好的结果,并且仍然可以使用HTML格式,将文档声明为HTML,但仍然保持它们“干净”(格式良好)。我认为我们不应该宣称每一个都“更好”;他们可以共存。我们只是在做正确的事情。

#8


0  

An example of a difference is the break tag.

一个不同的例子是break标记。

<br>

is proper HTML, but in XHTML where the elements need to validate as XML, you must self-close the tag:

是适当的HTML,但在需要将元素验证为XML的XHTML中,必须自关闭标记:

<br />

#1


15  

This is probably the best article I've read on the differences and relative merits of each:

这可能是我读过的最好的一篇关于每一种方法的不同和相对优点的文章:

HTML Versus XHTML

Which should we use, HTML or XHTML, and why?

我们应该使用HTML还是XHTML,为什么呢?

There is also a rather technical comparison on the WHATWG wiki.

在WHATWG wiki上还有一个相当技术性的比较。

#2


14  

XHTML is based on XML, and thus requires the source to be well-formed. Since XHTML is more strict than HTML, less pre-processing is needed by the rendering engine.

XHTML基于XML,因此要求源代码格式良好。由于XHTML比HTML更严格,因此呈现引擎需要的预处理更少。

XHTML should be served as application/xhtml+xml for you to take advantage of the benefits, otherwise XHTML will be treated as ordinary HTML. Serving it as 'application/xhtml+xml' is not common on the web due to Internet Explorer, which cannot handle XHTML.

XHTML应该作为应用程序/ XHTML +xml来使用,否则XHTML将被视为普通的HTML。由于Internet Explorer不能处理xhtml,因此在web上作为“应用程序/xhtml+xml”服务并不常见。

#3


9  

The difference is that XHTML is based on XML while HTML is based on SGML.

不同之处在于XHTML是基于XML的,而HTML是基于SGML的。

Browsers use the SGML parser for content sent with the content type text/html and the XML parser for application/xhtml+xml.

浏览器使用SGML解析器处理与内容类型文本/html一起发送的内容,使用XML解析器处理应用程序/xhtml+ XML。

When using the SGML parser, browsers will continue the parsing even when they encounter a syntax error in the file. This is why so many people think they are doing XHTML when they are sending XML-looking files to the browser. This is actually a mistake since this will trigger many parsing errors in the browser and slow the rendering process.

当使用SGML解析器时,浏览器将继续解析,即使在文件中遇到语法错误。这就是为什么很多人在向浏览器发送xml格式的文件时认为他们在做XHTML。这实际上是一个错误,因为这将在浏览器中触发许多解析错误并减慢呈现过程。

When using the XML parser, browser will stop the parsing when a syntax error is encountered and display an XML error. This is of course only true for browsers that have an XML parser for HTML content, which is not true for Internet Explorer that will only download the file and not display it.

当使用XML解析器时,当遇到语法错误时,浏览器将停止解析并显示XML错误。当然,这只适用于具有HTML内容XML解析器的浏览器,而不适用于只下载文件而不显示文件的Internet Explorer。

What should you do then?

那你该怎么办呢?

You should use XHTML if you need the syntax checking and the strict structure that XML impose. You should keep in mind that you must be sending your XHTML content to Internet Explorer with the wrong content type with all the issues implied. You should also keep in mind that your document will break whenever an unvalid content is in the file, so you must take great care to sanitize any user input.

如果需要语法检查和XML强加的严格结构,则应该使用XHTML。您应该记住,您必须以错误的内容类型将XHTML内容发送到Internet Explorer,其中包含所有的问题。您还应该记住,当文件中有无效内容时,您的文档将会中断,因此您必须非常小心地处理任何用户输入。

You should use HTML for anything else. HTML just works and is a greatly supported standard today. Even the next standard HTML 5 defines an SGML-based syntax so this will last.

您应该使用HTML进行其他操作。HTML仅仅是有效的,并且是当今非常受支持的标准。甚至下一个标准的HTML 5也定义了一个基于sgml的语法,因此这种语法将持续下去。

Keep in mind that whatever the format you choose, validating your output is always a good idea to detect syntax error.

请记住,无论选择何种格式,验证输出始终是检测语法错误的好方法。

#4


8  

The Extensible Hypertext Markup Language, or XHTML, is a markup language that has the same depth of expression as HTML, but also conforms to XML syntax.

可扩展超文本标记语言(或XHTML)是一种标记语言,具有与HTML相同的表达深度,但也符合XML语法。

XHTML is "the modern version of HTML 4".

XHTML是“HTML 4的现代版本”。

More info : wikipedia and W3C

更多信息:*和W3C

#5


3  

As previously stated, XHTML should be (in theory) valid XML. In theory (but not in practice) non-conforming XHTML should not be rendered by the browser.

如前所述,XHTML(理论上)应该是有效的XML。在理论上(但不是在实践中),不符合的XHTML不应该由浏览器呈现。

There is very rarely an advantage to use one over the other, so write whichever you personally prefer - as long as you do it consistently. HTML5 which is going to be the future web-standard will support both XHTML and HTML 4.01-style tags (the prior via optional "XML serialization"), and XHTML2 is looking like it will be pretty much dead in the water with no vendors actively supporting it.

使用一种方法比使用另一种方法几乎没有什么好处,所以你可以用你自己喜欢的方法来写作——只要你坚持这样做。HTML5将成为未来的web标准,它将支持XHTML和HTML 4.01样式的标签(之前通过可选的“XML串行化”),而XHTML2看起来将会在没有供应商积极支持它的情况下死水一潭。

#6


2  

Summary: use HTML.

摘要:使用HTML。

There are many differences, and many smart people (that know what they are talking about :D) have already summed up most of the pros and cons.

有很多不同之处,很多聪明人(他们知道自己在说什么:D)已经总结出了大多数的利弊。

#7


1  

It's not about which one is better; the HTML still continues to develop, and HTML5 is on its way (for years, though :) ) and delivers some new elements for easier management of new technologies like multimedia in web pages.

不是哪个更好;HTML仍在继续开发,HTML5也在发展中(尽管已经有很多年了),并交付了一些新元素,以便在web页面中更容易地管理多媒体等新技术。

On the other hand, XHTML is about strictness of XML, should we say keeping things clean. If you keep your HTML document well-formed (close every opened element, keep things nested in the tree-form), you're getting the best out of XHTML/XML world and can still be using the HTML format, declaring your documents to be HTML, but still keeping them 'clean' (well-formed). I don't think we should declare each of them 'better'; they can co-exist. It's just about us doing our things the right way.

另一方面,XHTML是关于XML的严格性的,我们应该说保持事物的整洁。如果您保持HTML文档格式良好(关闭每个打开的元素,将其嵌套在树状中),那么您将从XHTML/XML世界中得到最好的结果,并且仍然可以使用HTML格式,将文档声明为HTML,但仍然保持它们“干净”(格式良好)。我认为我们不应该宣称每一个都“更好”;他们可以共存。我们只是在做正确的事情。

#8


0  

An example of a difference is the break tag.

一个不同的例子是break标记。

<br>

is proper HTML, but in XHTML where the elements need to validate as XML, you must self-close the tag:

是适当的HTML,但在需要将元素验证为XML的XHTML中,必须自关闭标记:

<br />