What are the main differences between XHTML and HTML? [closed]

假如想象 提交于 2019-12-29 12:12:19

问题


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


回答1:


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?

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




回答2:


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




回答3:


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

Browsers use the SGML parser for content sent with the content type text/html and the XML parser for application/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.

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.

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.

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.

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




回答4:


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 is "the modern version of HTML 4".

More info : wikipedia and W3C




回答5:


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.

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.




回答6:


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.

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.




回答7:


Summary: use 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.




回答8:


An example of a difference is the break tag.

<br>

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

<br />


来源:https://stackoverflow.com/questions/195840/what-are-the-main-differences-between-xhtml-and-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!