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

Unit 13: Extensible Markup Language



            13.5 Transforming XML with XSLT                                                       Notes

            Extensible Stylesheet Language Transformations (XSLT) is a language for transforming XML
            documents into different XML, HTML, or any other format. For example, many web sites offer
            several formats of their content.

            HTML, printable HTML, and WML (Wireless Markup Language) are common. The easiest way
            to present these multiple views of the same information is to maintain one form of the content
            in XML and use XSLT to produce the HTML, printable HTML, and WML.
            PHP’s XSLT support is still experimental at the time of writing, and the exact implementation
            details may change from what is described here. However, this description should give you a
            good foundation for how to use PHP’s XSLT functions, even if the implementation changes in
            the future.
            Three documents are involved in an XSLT transformation: the original XML document, the XSLT
            document  containing  transformation  rules,  and  the  resulting  document.  The  final  document
            does not have to be in XML. A common use of XSLT is to generate HTML from XML. To do an
            XSLT transformation in PHP, you create an XSLT processor, give it some input to transform,
            and then destroy the processor.
            Create a processor with xslt_create( ):
            $xslt = xslt_create( );
            Process a file with xslt_process( ):

            $result = xslt_process(xslt, xml, xsl [, result [, arguments [, parameters ]]]);
            The xml and xsl parameters are filenames for the input XML and transformation XSL, respectively.
            Specify a result filename to store the new document in a file, or omit it to have xslt_process( )
            return the new document. The parameters option is an associative array of parameters to your
            XSL, accessible through xsl:param name=”parameter_name”.
            The arguments option is a roundabout way of working with XML or XSL stored in variables
            rather than in files. Set xml or xsl to ‘arg:/foo’, and the value for /foo in the arguments associative
            array will be used as the text for the XML or XSL document.

            Given example is the XML document we are going to transform. It is in a similar format to
            many of the news documents you find on the Web.

                          XML document
            <?xml version=”1.0” ?>

            <news xmlns:news=”http://example.org/backslash.dtd”>
            <story>
            <title>xy Publishes Programming PHP</title>
            <url>http://example.org/abes.php?id=20020430/458566</url>
            <time>2002-04-30 09:04:23</time>

            <author>ABC</author>
            </story> <story>
            <title>Transforming XML with PHP Simplified</title>
            <url>http://example.org/abes.php?id=20020430/458566</url>




                                             LOVELY PROFESSIONAL UNIVERSITY                                   335
   336   337   338   339   340   341   342   343   344   345   346