Displaying Versus Parsing In XHTML

In the heyday of HTML, browsers were designed specifically to display the results of HTML code that they were given. Those browsers displayed HTML through a set of hard-coded rules: for example, “at the appearance of <p>, skip a line and continue dis- playing the content.” Rules such as these allowed HTMLauthors to take “legal” short- cuts—or, more accurately, to write plain, sloppy code. According to the rule just stated, there is no need to close a paragraph using </p> as long as the next one starts with <p>—the rule will work and the content will be displayed properly. This is not the case with XML—and remember, XHTML is an XML language. 



XML documents are parsed before being displayed. Since XML languages can contain ele- ments and attributes that the browser may not already be familiar with, all elements and attributes must be checked before being displayed. An XML parser does this checking; it checks to make sure that the document is well formed. (Later in this chap- ter we will cover all of the requirements of a well-formed document.) 



Usually, the first difference between XHTML and HTML that authors notice is that, in XHTML, all ele- ments must be properly “closed.” Thus, not only must you start each paragraph with <p>, but you must also end each paragraph with </p>. If you do not, the parser will emit an error and stop processing the document. Parsing a document enforces good document structure. Think about an engineer designing a building. If the building is not structurally sound, there is no reason to build it, let alone live in it. Parsers ensure that documents are structurally sound before they are displayed to the user.

No comments:

Post a Comment