XML
  Home arrow XML arrow Page 5 - XSL Transformation With PHP And Sablot...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XSL Transformation With PHP And Sablotron
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2001-11-30

    Table of Contents:
  • XSL Transformation With PHP And Sablotron
  • Getting Down To Business
  • Start It Up
  • Handling Things Better
  • An Evening At The Moulin Rouge
  • Mistakes Happen
  • Publish Or Die!
  • Endzone

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    XSL Transformation With PHP And Sablotron - An Evening At The Moulin Rouge


    (Page 5 of 8 )

    What you've just seen is a generic way to process XML data with PHP. It isn't the only one, though; for larger, more complex files, PHP offers yet another method of applying an XSLT stylesheet to your XML.

    Consider the following XML file:

    <?xml version="1.0"?>




    <review id="57" category="2">




    <title>Moulin Rouge</title>




    <cast>
    <person>Nicole Kidman</person>
    <person>Ewan McGregor</person>

    <person>John
    Leguizamo</person>
    <person>Jim Broadbent</person>

    <person>Richard
    Roxburgh</person>
    </cast>




    <director>Baz Luhrmann</director>




    <duration>120</duration>




    <genre>Romance/Comedy</genre>




    <year>2001</year>




    <body>
    A stylishly spectacular extravaganza, <title>Moulin Rouge</title>
    is hard
    to categorize; it is, at different times, a love story, a costume drama,
    a
    musical, and a comedy. Director <person>Baz Luhrmann</person> (well-known
    for
    the very hip <title>William Shakespeare's Romeo + Juliet</title>) has
    taken
    some simple themes - love, jealousy and obsession - and done
    something completely
    new and different with them by setting them to music.
    </body>




    <rating>5</rating>




    <teaser>Baz Luhrmann's over-the-top
    vision of Paris at the turn of the
    century
    is witty, sexy...and completely unforgettable</teaser>




    </review>

    And the corresponding XSLT stylesheet to transform this information into a simple Web page:

    <?xml version="1.0"?>




    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">




    <xsl:template match="/review">
    <html>
    <head>
    <basefont face="Arial"
    size="2"/>
    </head>
    <body>
    <xsl:apply-templates select="title"/> (<xsl:apply-templates
    select="year"/>)
    <br
    />
    <xsl:apply-templates select="teaser"/>
    <p />
    <xsl:apply-templates
    select="cast"/>
    <br />
    <xsl:apply-templates select="director"/>
    <br
    />
    <xsl:apply-templates select="duration"/>
    <br />
    <xsl:apply-templates
    select="rating"/>
    <p>
    <xsl:apply-templates select="body"/>
    </p>
    </body>
    </html>
    </xsl:template>




    <xsl:template match="title">
    <b><xsl:value-of select="." /></b>
    </xsl:template>




    <xsl:template match="teaser">
    <xsl:value-of select="." />
    </xsl:template>




    <xsl:template match="director">
    <b>Director: </b> <xsl:value-of
    select="." />
    </xsl:template>




    <xsl:template match="duration">
    <b>Duration: </b> <xsl:value-of
    select="." /> minutes
    </xsl:template>




    <xsl:template match="rating">
    <b>Our rating: </b> <xsl:value-of
    select="." />
    </xsl:template>




    <xsl:template match="cast">
    <b>Cast: </b>
    <xsl:apply-templates/>
    </xsl:template>




    <xsl:template match="person[position() != last()]">
    <xsl:value-of select="."
    />,
    </xsl:template>




    <xsl:template match="person[position() = (last()-1)]">
    <xsl:value-of select="."
    />
    </xsl:template>




    <xsl:template match="person[position() = last()]">
    and <xsl:value-of select="."
    />
    </xsl:template>




    <xsl:template match="body">
    <xsl:apply-templates/>
    </xsl:template>




    <xsl:template match="body//title">
    <i><xsl:value-of select="." /></i>
    </xsl:template>




    <xsl:template match="body//person">
    <b><xsl:value-of select="." /></b>
    </xsl:template>




    </xsl:stylesheet>

    Here's the PHP script which brings the two together:

    <?php




    // XML file
    $xmlstringarray = file("movie.xml");




    // XSL file
    // note that you may need to specify an absolute path here
    $xslfile
    = "movie.xsl";




    // start transforming...
    xslt_output_begintransform($xslfile);




    for($count=0; $count<sizeof($xmlstringarray); $count++)
    {
    echo $xmlstringarray[$count];
    }




    // end transformation and output
    xslt_output_endtransform();
    ?>

    I'll start by storing the content of the XML file in an array.

    // XML file
    $xmlstringarray = file("movie.xml");

    Next, I'll store the location of the XSLT file in a variable.

    // XSL file
    // note that you may need to specify an absolute path here
    $xslfile
    = "movie.xsl";

    Finally, it's time to call the xslt_output_begintransform() function.

    xslt_output_begintransform($xslfile);




    for($count = 0;$count < sizeof($xmlstringarray); $count++)
    {
    echo $xmlstringarray[$count];
    }




    xslt_output_endtransform();

    The xslt_output_begintransform() function is a special function which take only one parameter: the location of the XSLT file. Once this function is invoked, the XSLT processor will transform all the content that it encounters as per the rules in the named file until it's told to stop via the function xslt_output_endtransform().

    In this case, I've written a simple "for" loop to iterate through the elements of the XML array, printing each element as it finds it. The XSLT processor will transform this output as it is printed, providing the desired result.



    This technique comes in particularly handy if you don't have your XML data in a static file, but generate it dynamically - say, from a database, or by combining multiple XML documents into a single file. You can write PHP code to obtain the data and massage it into an XML-compliant format, and then enclose that code in calls to xslt_output_begintransform() and xslt_output_endtransform() to transform it.

    More XML Articles
    More By Harish Kamath, (c) Melonfire


     

       

    XML ARTICLES

    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway