What's the need for XHTML?

2019-01-17 09:49发布

In an interview I was asked a question that I'd never thought about, which was "We already have HTML which fulfills all the requirements of writing a web page, so what's the need for XHTML?"

I Googled a lot and also read many articles, but I'm not able to get properly why XHTML has been introduced. Please explain to me.

标签: html css xhtml
16条回答
别忘想泡老子
2楼-- · 2019-01-17 10:46

XHTML is an attempt to encourage the development of "well-formed" HTML.

HTML has evolved over more than 10 years. Its implementation, and the implementation of the browsers that parse and render it, are not exactly consistent. This is why cross-browser compatibility is a major headache.

HTML is based on SGML (Standard Generalized Markup Language.) XML is also derived from SGML, so they are cousins of a sort. XHTML marries the two, providing (in theory) the benefits of XML to HTML. This includes a well-defined schema that can be reliably validated, queried, and transformed.

查看更多
三岁会撩人
3楼-- · 2019-01-17 10:47

Because it is valid XML. That helps a lot since you can use a lot of tools originally designed for XML, such as XML parsers, XSLT, XPath, XQuery, ...

Normal HTML is a SGML dialect and that is not parsable without knowledge of the schema.

<ul>
    <li>one
    <li>two
    <li>three
</ul>

is correct HTML but not correct XML. If you want to parse that, you have to know that ul-elements have to be closed but li s don't.

查看更多
地球回转人心会变
4楼-- · 2019-01-17 10:47

XHTML also allows you to embed other XML dialects like MathML, Ruby, SVG, etc. (You can also embed XHTML in other XML dialects, if desired.)

If you are just 'making a web page', you don't necessarily need XHTML. But if you are programmatically generating a page, you might find that the tools for generating XML are better than those that generate HTML.

查看更多
贼婆χ
5楼-- · 2019-01-17 10:47

In a nutshell: XHTML is often only beneficial and preferred over HTML whenever you want to use a XML based tool to manipulate/transform/generate HTML pages on the server side.

Lot of examples can be found in component based MVC frameworks like Sun Oracle JSF which uses Facelets as a XHTML based view technology. The server side components are definied in XSD's and the pages are parsed using a SAX parser. You can even add a <!DOCTYPE html> to top of the page to let Facelets generate "pure" valid and strict HTML5. Microsoft ASP.NET MVC has a similar view technology.

When you're hand-writing HTML, XHTML doesn't add much benefit, or it must be pushing off the "coolness" of using a (over)hyped technology.

See also:

查看更多
登录 后发表回答