Chapter 4: Data Formats 155
PART I
Well-formedness only addresses if an XML response is correct in its basic syntax, but it
says nothing about whether the XML packet follows any particular grammar; that is the
domain of validity.
Valid XML
An important characteristic of XML is that not only does it enforce strict syntax, but the format
also allows you to enforce document semantics. The process of checking a document against
what is allowed in a particular language is the process of validation. Valid documents must not
only be well formed, they must also conform to a grammar defined either in Document Type
Definition (DTD) or Schema (XSD). The ability to validate response data would seem quite
useful in Ajax as you would then validate received XML packets not just for syntax but to make
sure they are in the expected structure before you attempt to extract any contents. Unfortunately,
the ability to validate XML in browsers is severely limited at the time of this book’s writing.
In the previous example, well-formed markup has just been passed back, but we could
have easily put a DTD or schema in the packet. For example, here the standard response
packet is generated but an internal DTD defining the packet structure is added in:
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-Type: text/xml");
FIGURE 4-6 Testing XML handling in browsers