Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

PHP and the Sablotron Processor
Pages: 1, 2, 3

The final and perhaps most important piece of code is shown in Listing 1-5. This is the PHP code that calls the Sablotron functions that perform the XSL transformation.

Listing 1-5: The PHP/Sablotron script (transform.php)



<?php

// Include the DeviceType() function.
INCLUDE("xmllib.inc");


// What are the XML and XSL files?
$xmlFile = "news.xml";
$xslFile = DeviceType();

// Create a new processor handle
$th = @xslt_create() or die("Can't create XSLT handle!");

// Open the XML and XSL files
$xh = fopen($xmlFile, "r") or die("Can't open XML file");
$sh = fopen($xslFile, "r") or die("Can't open XSL file");

// Read in the XML and XSL contents
$xmlContent = fread($xh, filesize($xmlFile));
$xslContent = fread($sh, filesize($xslFile));

// Perform the XSL transformation
@xslt_process($xslContent, $xmlContent, $XSLtransformation);

// send the correct WAP header if necessary
if ($xslFile == "wapnews") :

header("content-type: text/vnd.wap.wml");

endif;

// Output the transformed XML file
echo $XSLtransformation;

// Free up the resources
@xslt_free($th);

?>

That's all there is to it! After executing Listing 1-5 using the news.xml and pcnews.xsl documents, you will see an output very similar to the one shown in Figure 1.

Screenshot of resulting output.
Figure 1. News.xml transformed using pcnews.xsl.

Figure 2 displays how news.xml might look in the typical WAP browser. In this example, I used Phone.com's UP.SDK browser.

Results viewed with a WAP browser.
Figure 2. news.xml transformed using wapnews.xsl

Conclusion

There's no doubt about it, the Sablotron extension is awesome. Using it, XSL transformations are a snap. If you end up doing anything cool with it, please send me your feedback; I'd love to hear about it.

W.J. Gilmore has been developing PHP applications since 1997, and is frequently published on the subject within some of the Web's most popular development sites. He is the author of 'A Programmer's Introduction to PHP 4.0' (January 2001, Apress), and is the Assistant Editorial Director of Web and Open Source Technologies at Apress.


Return to ONLamp.com.




-->