PHP 5.6.29 Released

xslt_process

(PHP 4 >= 4.0.3)

xslt_processPerform an XSLT transformation

Description

mixed xslt_process ( resource $xh , string $xmlcontainer , string $xslcontainer [, string $resultcontainer [, array $arguments [, array $parameters ]]] )

The xslt_process() function is the crux of the XSLT extension. It allows you to perform an XSLT transformation using almost any type of input source - the containers. This is accomplished through the use of argument buffers -- a concept taken from the Sablotron XSLT processor (currently the only XSLT processor this extension supports). The input containers default to a filename 'containing' the document to be processed.

Parameters

xh

The XSLT processor link identifier, created with xslt_create().

xmlcontainer

Path to XML file or placeholder for the XML argument.

xslcontainer

Path to XSL file or placeholder for the XML argument.

resultcontainer

The result container defaults to a filename for the transformed document. If the result container is not specified - i.e. NULL - than the result is returned.

arguments

Instead of files as the XML and XSLT arguments to the xslt_process() function, you can specify "argument place holders" which are then substituted by values given in the arguments array.

parameters

An array for any top-level parameters that will be passed to the XSLT document. These parameters can then be accessed within your XSL files using the <xsl:param name="parameter_name"> instruction. The parameters must be UTF-8 encoded and their values will be interpreted as strings by the Sablotron processor. In other words - you cannot pass node-sets as parameters to the XSLT document.

Containers can also be set via the arguments array (see below).

Return Values

Returns TRUE on success or FALSE on failure. If the result container is not specified - i.e. NULL - than the result is returned.

Changelog

Version Description
4.0.6 This function no longer takes XML strings in xmlcontainer or xslcontainer. Passing a string containing XML to either of these parameters will result in a segmentation fault in Sablotron versions up to and including version 0.95.

Examples

The simplest type of transformation with the xslt_process() function is the transformation of an XML file with an XSLT file, placing the result in a third file containing the new XML (or HTML) document. Doing this with Sablotron is really quite easy...

Example #1 Using the xslt_process() to transform an XML file and a XSL file to a new XML file

<?php

// Allocate a new XSLT processor
$xh xslt_create();

// Process the document
if (xslt_process($xh'sample.xml''sample.xsl''result.xml')) {
    echo 
"SUCCESS, sample.xml was transformed by sample.xsl into result.xml";
    echo 
", result.xml has the following contents\n<br />\n";
    echo 
"<pre>\n";
    
readfile('result.xml');
    echo 
"</pre>\n";
} else {
    echo 
"Sorry, sample.xml could not be transformed by sample.xsl into";
    echo 
"  result.xml the reason is that " xslt_error($xh) . " and the ";
    echo 
"error code is " xslt_errno($xh);
}

xslt_free($xh);

?>

While this functionality is great, many times, especially in a web environment, you want to be able to print out your results directly. Therefore, if you omit the third argument to the xslt_process() function (or provide a NULL value for the argument), it will automatically return the value of the XSLT transformation, instead of writing it to a file...

Example #2 Using the xslt_process() to transform an XML file and a XSL file to a variable containing the resulting XML data

<?php

// Allocate a new XSLT processor
$xh xslt_create();

// Process the document, returning the result into the $result variable
$result xslt_process($xh'sample.xml''sample.xsl');
if (
$result) {
    echo 
"SUCCESS, sample.xml was transformed by sample.xsl into the \$result";
    echo 
" variable, the \$result variable has the following contents\n<br />\n";
    echo 
"<pre>\n";
    echo 
$result;
    echo 
"</pre>\n";
} else {
    echo 
"Sorry, sample.xml could not be transformed by sample.xsl into";
    echo 
"  the \$result variable the reason is that " xslt_error($xh);
    echo 
" and the error code is " xslt_errno($xh);
}

xslt_free($xh);

?>

The above two cases are the two simplest cases there are when it comes to XSLT transformation and I'd dare say that they are the most common cases, however, sometimes you get your XML and XSLT code from external sources, such as a database or a socket. In these cases you'll have the XML and/or XSLT data in a variable -- and in production applications the overhead of dumping these to file may be too much. This is where XSLT's "argument" syntax, comes to the rescue. Instead of files as the XML and XSLT arguments to the xslt_process() function, you can specify "argument place holders" which are then substituted by values given in the arguments array (5th parameter to the xslt_process() function). The following is an example of processing XML and XSLT into a result variable without the use of files at all.

Example #3 Using the xslt_process() to transform a variable containing XML data and a variable containing XSL data into a variable containing the resulting XML data

<?php
// $xml and $xsl contain the XML and XSL data

$arguments = array(
     
'/_xml' => $xml,
     
'/_xsl' => $xsl
);

// Allocate a new XSLT processor
$xh xslt_create();

// Process the document
$result xslt_process($xh'arg:/_xml''arg:/_xsl'NULL$arguments);
if (
$result) {
    echo 
"SUCCESS, sample.xml was transformed by sample.xsl into the \$result";
    echo 
" variable, the \$result variable has the following contents\n<br />\n";
    echo 
"<pre>\n";
    echo 
$result;
    echo 
"</pre>\n";
} else {
    echo 
"Sorry, sample.xml could not be transformed by sample.xsl into";
    echo 
"  the \$result variable the reason is that " xslt_error($xh);
    echo 
" and the error code is " xslt_errno($xh);
}
xslt_free($xh);
?>

Example #4 Passing PHP variables to XSL files

<?php

// XML string
$xml '<?xml version="1.0"?>
<para>
 change me
</para>'
;

// XSL string
$xsl '
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1" indent="no"
 omit-xml-declaration="yes"  media-type="text/html"/>
 <xsl:param name="myvar"/>
 <xsl:param name="mynode"/>
 <xsl:template match="/">
My PHP variable : <xsl:value-of select="$myvar"/><br />
My node set : <xsl:value-of select="$mynode"/>
 </xsl:template>
</xsl:stylesheet>'
;


$xh xslt_create();

// the second parameter will be interpreted as a string
$parameters = array (
  
'myvar' => 'test',
  
'mynode' => '<foo>bar</foo>'
);

$arguments = array (
  
'/_xml' => $xml,
  
'/_xsl' => $xsl
);

echo 
xslt_process($xh'arg:/_xml''arg:/_xsl'NULL$arguments$parameters);

?>

The above example will output:

My PHP variable : test<br>
My node set : &lt;foo&gt;bar&lt;/foo&gt;

Notes

Note:

Please note that file:// is needed in front of the path when using Windows.

add a note add a note

User Contributed Notes 7 notes

up
1
Anonymous
11 years ago
just to let you know an an apostrophe   can generate a  "not well-formed (invalid token)" error message. I spent a couple of hours to find it, until i found another user with similar problem:
http://www.feedforall.com/forum/viewtopic.php?t=116&view=next
lets hope that hosting providers will soon adopt php5
up
0
ohlesbeauxjours at yahoo dot fr
13 years ago
Thanks, Martin !

As a matter of fact, your way of doing is probably more logical than mine :)
The point is that scheme handler strangely adds a slash to your URI , if it doesn't start by any slash.

So if you call : document('http:www.url.com',page/@url)
...then $rest gets /www.url.com

But if you call : document('http://www.url.com',page/@url)
...then $rest gets //www.url.com

Another approach could consist in enclosing the scheme "http" in the XML attribute : <page url="http://www.w3.org/TR/REC-xml">
and simply doing a : $file = fopen("http:".$rest,"r");

It all depends on the context...

See also :
http://archive.gingerall.cz/archives/public/sablot2003/msg00598.html
up
0
martin_a_thomas at yahoo dot com
13 years ago
I had a little trouble making the http scheme handler work until I removed the line with the strip function and then changed the fopen
from 'http://' to 'http:' -

$file = fopen("http:".$rest,"r");

Regards // Martin
up
0
tom at hubalek dot net
14 years ago
Hi,

keep in mind that fifth parameter of function xsl_process() is __arguments__ and sixth is __xsl parameters__. I spent a lot of time learning difference between arguments and parameters ;-)

Tom
up
0
msopacua at idg dot nl
14 years ago
Took me a while to figure this one out, but no matter what your stylesheet encoding and xml encoding says - argument values, need to be UTF-8.

Version 4.2.1 and sablot 0.90.
up
0
chris_coupal at hotmail dot com
14 years ago
If you are getting the data from a database and creating the XML data stream for transformation, you can add the parameters for the stylesheet into the XML data. Once in the data, you can reference them from the XSL stylesheet directly.
up
0
ascii_NO_SPAM_ at _NO_SPAM_microcore dot dk
14 years ago
I noticed a slight difference in html output when upgrading to php4.1.1 on Windows.

I found the garbled output to be caused by [indent="yes"] in <xsl:output /> and simply changed it to [indent="no"].
To Top