Hudzilla.org - the homepage of Paul Hudson
Contents > XML & XSLT > Transforming XML using XSLT Wish List | Report Bug | About Me ]

12.4.2     Handling the processed output

This is NOT the latest copy of this book; click here for the latest version.

There are two ways to deal with the output of xslt_process(). One way is to use the optional fourth parameter that allows you to specify the name of a file you wish to save the processed content to. If you do not provide this parameter, or it is NULL, then xslt_process() returns the processed content via its return value.

The option you choose is obviously dependent on your plans for the output. Usually you will probably want to ignore the fourth parameter (or supply NULL) in order to get the content back in the function return value. This is done by capturing the return value in a variable, or by passing it straight to an print statement. Using our previous example, here is what that would look like. Save it as xslt_test.php:

<?php
    $xsltproc
=xslt_create();
    
$xslt_result = xslt_process($xsltproc,'/path/to/input.xml','/path/to/input.xsl');
    
xslt_free($xsltproc);
?>

...[snip]...

<?php
    
print $xslt_result;
?>

The difference here is that we capture the return value of xslt_process() in $xslt_result, which we then print out later on the script. While it is just as good in this situation to replace the "$xslt_result = " line with just print, you may wish to replace the dots between the xslt_process() and the printing out of the results with more PHP code. Note that there is no need to add an <HTML> tag in the PHP output because XSL is designed to add <HTML> and other basic tags if it matches a root element.

Go ahead and run the xslt_test.php script. If everything has gone as planned, you should be see your formatted XML returned. Also, take a look at the source of the page and note that it is all nicely-formatted XHTML.





<< 12.4.1 Adding PHP to the mix   12.4.3 Making XSL work for its money >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 07 Sep 2008

you need to...

run configure with the --enable-xslt --with-xslt-sablot options. The Sablotron library should be installed somewhere your compiler can find it.

Otherwise you get a:

Call to undefined function xslt_create()



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow