Page 327 - Open Soource Technologies 304.indd
P. 327

Unit 13: Extensible Markup Language



            Tags can be nested but cannot overlap. For example, this is valid:                    Notes
            <book><title>Programming PHP</title></book>
            but this is not valid, because the book and title tags overlap:

            <book><title>Programming PHP</book></title>
            XML also requires that the document begin with a processing instruction that identifies the version
            of XML being used (and possibly other things, such as the text encoding used). For example:


            <?xml version=”1.0” ?>

            The final requirement of a well-formed XML document is that there be only one element at the
            top level of the file. For example, this is well formed:

            <?xml version=”1.0” ?> <library> <title>Programming PHP</title> <title>Programming Perl</
            title> <title>Programming C#</title> </library>

            but this is not well formed, as there are three elements at the top level of the file:
            <?xml version=”1.0” ?> <title>Programming PHP</title> <title>Programming Perl</title>
            <title>Programming C#</title>

            There are two ways to write down this structure: the Document Type Definition (DTD) and the
            Schema. DTDs and Schemas are used to validate documents; that is, to ensure that they follow
            the rules for their type of document.
            Most XML documents do not include a DTD. Many identify the DTD as an external with a line
            that gives the name and location (file or URL) of the DTD:
            <! DOCTYPE rss PUBLIC ‘My DTD Identifier’ ‘http: //www.example.com/my.dtd’>
            Sometimes it is convenient to encapsulate one XML document in another. For example, an XML
            document representing a mail message might have an attachment element that surrounds an
            attached file. If the attached file is XML, it is a nested XML document. What if the mail message
            document has a body element (the subject of the message), and the attached file is an XML
            representation of a dissection that also has a body element, but this element has completely
            different DTD  rules? How can you possibly validate or make sense  of the document  if the
            meaning of body changes partway through?
            This problem is solved with the use of namespaces. Namespaces let you qualify the XML tag
            for example, email:body and human:body.
                          XML  documents  generally  are  not  completely  ad  hoc.  The  specific  tags,
                          attributes, and entities in an XML document, and the rules governing how
                          they nest, comprise the structure of the document.

            13.3 Generating XML

            Just as PHP can be used to generate dynamic HTML, it can also be used to generate dynamic
            XML. You can generate XML for other programs to consume based on forms, database queries,
            or anything else you can do in PHP. One application for dynamic XML is Rich Site Summary
            (RSS), a file format for syndicating news sites.
            Generating an XML document from a PHP script is simple. Simply change the MIME type of the
            document, using the header ( ) function, to “text/xml”. To emit the <?xml ... ?> declaration without
            it being interpreted as a malformed PHP tag, you will need to either disable short_open_tag in



                                             LOVELY PROFESSIONAL UNIVERSITY                                   321
   322   323   324   325   326   327   328   329   330   331   332