Page 617 - Beginning PHP 5.3
P. 617

Chapter 19: Working with XML
                                    < quantity > 67 < /quantity >
                                    < description > < ![CDATA[Beetroots are lovely  &  purple]] > < /description >


                                 < /item >
                               < /stockList >

                           Notice the reference to the URL of an external DTD in the second line. This allows a piece of software to
                         validate the document by reading the DTD and making sure the document conforms to the DTD.
                         Of course, you could manually read through the document and compare it with the elements, attributes,
                         and other document components specified in the DTD, but many applications are available that
                         can automatically validate an XML document against a DTD or an XSD. And because the DTD or XSD is
                         available either directly in the document or online, it ’ s easy for these applications to perform the
                         validation function for you automatically as they parse the document.
                               It ’ s also possible to embed the contents of a DTD within the XML document itself, rather than referenc-
                             ing an external DTD. However, embedding is recommended only if the DTD is small.

                            DTDs are special documents written in Extended Backus - Naur Form (EBNF), which is not an XML language
                          and isn ’ t as easy to parse as XML. DTDs specify constraints on XML elements, attributes, content, and more.

                            XSDs serve the same purpose as DTDs, but are written in the XML - based XML Schema language, and as
                          such they can easily be processed using an XML parser. XSDs are also much more capable than DTDs for
                          defining detail in your elements and attributes (such as data type, range of values, and so forth) and are
                          therefore preferred over DTDs by many XML authors. However, XSDs are a complex topic that is out of
                          the scope of this book, so this chapter concentrates on DTDs instead.

                               If you ’ re interested in XSDs you can find more about them at  http://www.w3schools.com/
                             Schema/default.asp .


                           XHTML: An Example of DTDs in Action
                           As mentioned previously, anyone can author an XML document, and anyone can define a DTD or XSD
                         against which to validate an XML document. One well - known example of a DTD is XHTML, which is
                         HTML reformulated as XML. The XHTML DTD is essentially similar to the existing HTML DTD, with
                         very small modifications, and it defines all the elements, attributes, and other components allowed in an
                         XHTML document. The main difference between HTML and XHTML is the fact that an XHTML
                         document  —  being an XML document at heart  —  must conform to the XML specification, whereas
                         HTML documents are not required to do so.
                           To display an XHTML document as a regular Web page, the document must be well - formed, and also
                         validate against the XHTML DTD. In the next few sections you examine a portion of the DTD for
                         XHTML, learn how the DTD can be referenced in an XHTML document, explore XML namespaces, and
                         learn how to create an XHTML Web page.

                           The DTDs for XHTML

                           There are three DTDs for XHTML. They ’ re located at:
                            ❑       www.w3.org/TR/xhtml1/DTD/xhtml1 - strict.dtd


                            ❑         www.w3.org/TR/xhtml1/DTD/xhtml1 - transitional.dtd


                            ❑         www.w3.org/TR/xhtml1/DTD/xhtml1 - frameset.dtd




                                                                                                         579


                                                                                                      9/21/09   9:17:44 AM
          c19.indd   579
          c19.indd   579                                                                              9/21/09   9:17:44 AM
   612   613   614   615   616   617   618   619   620   621   622