20.11 Transforming XML with an XSL Template


You want to transform an XML document through an XSL template using PHP.

Technique

Make sure that you have compiled PHP with Sablotron XSL support. Then you can use the xslt_transform() function:

 <?php if (!xslt_transform($stylesheet, $xmlfile, "arg:/result" 0, 0, $result)) {     die(sprintf('Error [%d]: %s',                 xslt_errno(), xslt_error())); } print "The transformed document looks like:\n<br>\n"; print $result; ?> 

Comments

XSL is a language that enables you to describe how structured XML will be displayed. In the synopsis, we take an XML document ( $xmlfile ) and we transform it through a XSL stylesheet ( $stylesheet ). The third and fourth arguments to xslt_transform() are any optional parameters (third argument), and any arguments that you would have to the XSL processor (fourth argument).

The arguments, parameters, and function arguments should be associative arrays in the format "argument_name" => "argument_value" . Examine the following, which specifies the document's output to a file instead of a buffer:

 <?php $args = array("/_outputfile" => "test.html"); if (!xslt_transform($stylesheet, $xslfile, "arg:/_outputfile", 0, $args)) {     die(sprintf('Error [%d]: %s',                 xslt_error(), xslt_errno())); } print "Transformation saved in test.html"; ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net