XML
  Home arrow XML arrow Page 3 - 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 
Moblin 
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

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    XSL Transformation With PHP And Sablotron - Start It Up


    (Page 3 of 8 )

    Let's start with a simple example. Consider the following XML document:

    <?xml version="1.0"?>
    <me>
    <name>John Doe</name>
    <address>94,
    Main Street, Nowheresville 16463, XY</address>
    <tel>738 2838</tel>
    <email>johndoe@black.hole.com</email>
    <url>http://www.unknown_and_unsung.com/</url>
    </me>

    Now, let's suppose that I need to transform this XML document into an HTML document which looks like this:



    Here's the XSLT stylesheet to accomplish this transformation:

    <?xml version="1.0"?>




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




    <xsl:template match="/">




    <html>
    <head>
    </head>
    <body>
    <h1>Contact information for
    <b><xsl:value-of select="me/name" /></b></h1>




    <h2>Mailing address:</h2>
    <xsl:value-of select="me/address" />




    <h2>Phone:</h2>
    <xsl:value-of select="me/tel" />




    <h2>Email address:</h2>
    <xsl:value-of select="me/email" />




    <h2>Web site URL:</h2>
    <xsl:value-of select="me/url" />




    </body>
    </html>




    </xsl:template>




    </xsl:stylesheet>

    So I've got the XML data and the XSLT stylesheet. All that remains is to put them together - which is where PHP comes in. Take a look at the following script, which brings it all together:

    <?php




    // store XML and XSL content as variables
    $xmlstring = join('', file('person.xml'));
    $xslstring
    = join('', file('person.xsl'));




    // call the XSLT processor directly
    xslt_process($xslstring, $xmlstring, $result);




    // output the result
    echo $result;
    ?>

    Let's dissect this a little.

    1. First, I've used the file() function to read the contents of the XML document and XSLT stylesheet into an array, and then used PHP's very cool join() function to combine all the elements of the array (which correspond to lines from the file) into a single string. Each string is then stored as a variable.

    $xmlstring = join('', file('person.xml'));
    $xslstring = join('', file('person.xsl'));

    2. You're probably wondering why I bothered. Well, the xslt_process() function, which happens to be PHP's primary workhorse for this sort of thing, accepts three parameters: an XML data string, an XSLT data string, and a variable to hold the results of the transformation.

    xslt_process($xslstring, $xmlstring, $result);

    3. Once the processing is complete, and $result contains the output, all that's left is to print it.

    echo $result;

    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 1 hosted by Hostway