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条回答
ら.Afraid
2楼-- · 2019-01-17 10:21

I think that it helps browsers correctly display the html without making assumptions about where tags should be closed. Any time a browsers assumes something you know what happens.

查看更多
何必那么认真
3楼-- · 2019-01-17 10:21

XML is a data interchange format - this is perfect for building websites because after all we are dealing with information and this info needs to be crawled and understood by computers (such as search engines).

查看更多
叛逆
4楼-- · 2019-01-17 10:22

I am actually writing this to ask why the above three posts which speak about browser-consistence and well formed html have been voted down?

As it is known HTML is a industry standard. Browsers are implemented so that they render the marked up content as described in the HTML standard. Unfortunately there are areas that have not been well defined in HTML: what happens if user forgot a closing tag or what to do if a referred image is not found? some browsers use the 'alt' tag to have a place holder text item and some browsers display the 'alt' tag as a tool tip. The famous 'quirks' mode of browsers is a result of this lack of clarity. Because of this, it became quite possible that the same web page would display differently on different browsers.

Also as HTML usage grew there was one more problem: it was not extensible - there was no way to add user-defined tags.

XHTML solves the above problems:

  • adopt XML to provide extensible tags.
  • provide a 'strict' standard for web browsers

XHTML has well defined rules about the structure and these can be programatically enforced. Check the various online "XHTML Validators". They will tell if your XHTML is well formed or not (and highlight the problem areas). Because of these strict rules your page is more or less guaranteed to look the same on all browsers implementing XHTML.

[note] if you want to verify the above, please refer to the text "Head First XHTML and CSS"

查看更多
倾城 Initia
5楼-- · 2019-01-17 10:24

From Wiki:

Because they need to be well-formed, true XHTML documents allow for automated processing to be performed using standard XML tools—unlike HTML, which requires a relatively complex, lenient, and generally custom parser. XHTML can be thought of as the intersection of HTML and XML in many respects, since it is a reformulation of HTML in XML.

Having HTML conform to XML standards allows for a much more consistent parsing of the page. Whereas in HTML, for example, you were allowed to have tags out of order <b><u>test</b></u> now you can't, they must be closed in the order they were opened. Things like this make DOM parsing (which is now used heavily in AJAX) much easier.

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

Why was XHTML created?

  • HTML is not very extensible. XHTML aimed to fix this by introducing namespaces so that languages such as MathML or SVG could be included inline.
  • XMl is much simpler to parse than SGML (the format used by HTML before version 5)
  • Due to an overwhelming number of websites with errors, browsers attempted to correct incorrect markup. New browsers have had to attempt to correct it in the same way. XHTML tries to increase standards by specifying that only structurally correct code will display.

How well has it succeeded?

  • XHTML is widely spread, but almost always served with the text/html MIME type due to incompatibilities with Internet Explorer (up to version 8). Many of these pages would actually break if served as XML. So none of the three advantages above have really materialised.
  • Many people chose to use XHTML as they thought it would provide better future compatibility. Work has stopped on XHTML2.0 and while HTML5 will have an XHTML serialisation, this seems to be receiving minimal attention. XHTML provides no future compatibility advantages for the forseeable future. Mozilla and Safari recommend using just HTML.
  • HTML with a strict DTD already has a much cleaner format. HTML5 will take this further by removing the transitional DTD, removing unnecessary elements and defining a standard way for parsing documents with a degree of backwards compatibility. Browsers will still correct errors for the HTML serialisation, rather than forcing the markup to be fixed, but at least they will do it in the same way. Those who care about correct code will use validators anyway.

What is the need for XHTML?

XHTML had laudable goals and maybe it will be able to deliver in the future. I can't recommend XHTML for the possible future advantages it might provide, when HTML is much easier now. You should only really use XHTML if previous code or your tools force you to.

查看更多
劫难
7楼-- · 2019-01-17 10:24

Because XHTML makes a lot more sense!

The point is, even though something might not provide any more technical possibilities, it's still an improvement if it's remade just to be more clear and logical. That's why code refactoring is a good idea even if it doesn't change any of the functionality. That's why Brainfuck wound't be a good programming language, even if it had all the capabilities of Java.

XHTML makes more sense because the underlying structure of tags and their attributes is always consistent - not dependent on the tag semantics. The way it makes more sense is pretty evident, once you get familiar with its difference to HTML, but for example tags are always orderly nested, all tags must close, names must be lowercase, attribute values must have limiting characters around them.

查看更多
登录 后发表回答