<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Perform an XSLT transformation</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.xslt-getopt.html">xslt_getopt</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.xslt-set-base.html">xslt_set_base</a></div> <div class="up"><a href="ref.xslt.html">XSLT (PHP 4) Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.xslt-process" class="refentry"> <div class="refnamediv"> <h1 class="refname">xslt_process</h1> <p class="verinfo">(PHP 4 >= 4.0.3)</p><p class="refpurpose"><span class="refname">xslt_process</span> — <span class="dc-title">Perform an <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> transformation</span></p> </div> <div class="refsect1 description" id="refsect1-function.xslt-process-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>xslt_process</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$xh</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$xmlcontainer</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$xslcontainer</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$resultcontainer</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$arguments</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$parameters</code></span> ]]] )</div> <p class="para rdfs-comment"> The <span class="function"><strong>xslt_process()</strong></span> function is the crux of the <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> extension. It allows you to perform an <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> transformation using almost any type of input source - the containers. This is accomplished through the use of argument buffers -- a concept taken from the <span class="productname">Sablotron</span> <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> processor (currently the only <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> processor this extension supports). The input containers default to a filename 'containing' the document to be processed. </p> </div> <div class="refsect1 parameters" id="refsect1-function.xslt-process-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter"> xh</code></em></span><dd> <p class="para">The <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> processor link identifier, created with <span class="function"><a href="function.xslt-create.html" class="function">xslt_create()</a></span>.</p></dd> </dt> <dt> <span class="term"><em><code class="parameter">xmlcontainer</code></em></span> <dd> <p class="para"> Path to <acronym title="eXtensible Markup Language">XML</acronym> file or placeholder for the <acronym title="eXtensible Markup Language">XML</acronym> argument. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">xslcontainer</code></em></span> <dd> <p class="para"> Path to <acronym>XSL</acronym> file or placeholder for the <acronym title="eXtensible Markup Language">XML</acronym> argument. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">resultcontainer</code></em></span> <dd> <p class="para"> The result container defaults to a filename for the transformed document. If the result container is not specified - i.e. <strong><code>NULL</code></strong> - than the result is returned. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">arguments</code></em></span> <dd> <p class="para"> Instead of files as the <acronym title="eXtensible Markup Language">XML</acronym> and <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> arguments to the <span class="function"><strong>xslt_process()</strong></span> function, you can specify "argument place holders" which are then substituted by values given in the <em><code class="parameter">arguments</code></em> array. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">parameters</code></em></span> <dd> <p class="para"> 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 <em><xsl:param name="parameter_name"></em> instruction. The parameters must be UTF-8 encoded and their values will be interpreted as strings by the <span class="productname">Sablotron</span> processor. In other words - you cannot pass <em>node-sets</em> as parameters to the <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> document. </p> </dd> </dt> </dl> </p> <p class="para"> Containers can also be set via the <em><code class="parameter">arguments</code></em> array (see below). </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.xslt-process-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. If the result container is not specified - i.e. <strong><code>NULL</code></strong> - than the result is returned. </p> </div> <div class="refsect1 changelog" id="refsect1-function.xslt-process-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.0.6</td> <td> This function no longer takes XML strings in <em><code class="parameter">xmlcontainer</code></em> or <em><code class="parameter">xslcontainer</code></em>. 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. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.xslt-process-examples"> <h3 class="title">Examples</h3> <p class="para"> The simplest type of transformation with the <span class="function"><strong>xslt_process()</strong></span> function is the transformation of an <acronym title="eXtensible Markup Language">XML</acronym> file with an <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> file, placing the result in a third file containing the new <acronym title="eXtensible Markup Language">XML</acronym> (or HTML) document. Doing this with <span class="productname">Sablotron</span> is really quite easy... </p> <div class="example" id="example-5550"> <p><strong>Example #1 Using the <span class="function"><strong>xslt_process()</strong></span> to transform an <acronym title="eXtensible Markup Language">XML</acronym> file and a <acronym>XSL</acronym> file to a new <acronym title="eXtensible Markup Language">XML</acronym> file</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Allocate a new XSLT processor<br /></span><span style="color: #0000BB">$xh </span><span style="color: #007700">= </span><span style="color: #0000BB">xslt_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Process the document<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">xslt_process</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">, </span><span style="color: #DD0000">'sample.xml'</span><span style="color: #007700">, </span><span style="color: #DD0000">'sample.xsl'</span><span style="color: #007700">, </span><span style="color: #DD0000">'result.xml'</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into result.xml"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">", result.xml has the following contents\n<br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"<pre>\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">'result.xml'</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">"</pre>\n"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" result.xml the reason is that " </span><span style="color: #007700">. </span><span style="color: #0000BB">xslt_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">) . </span><span style="color: #DD0000">" and the "</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"error code is " </span><span style="color: #007700">. </span><span style="color: #0000BB">xslt_errno</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">xslt_free</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> 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 <span class="function"><strong>xslt_process()</strong></span> function (or provide a NULL value for the argument), it will automatically return the value of the <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> transformation, instead of writing it to a file... </p> <p class="para"> <div class="example" id="example-5551"> <p><strong>Example #2 Using the <span class="function"><strong>xslt_process()</strong></span> to transform an <acronym title="eXtensible Markup Language">XML</acronym> file and a <acronym>XSL</acronym> file to a variable containing the resulting XML data</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Allocate a new XSLT processor<br /></span><span style="color: #0000BB">$xh </span><span style="color: #007700">= </span><span style="color: #0000BB">xslt_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Process the document, returning the result into the $result variable<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">xslt_process</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">, </span><span style="color: #DD0000">'sample.xml'</span><span style="color: #007700">, </span><span style="color: #DD0000">'sample.xsl'</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into the \$result"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" variable, the \$result variable has the following contents\n<br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"<pre>\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">$result</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"</pre>\n"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" the \$result variable the reason is that " </span><span style="color: #007700">. </span><span style="color: #0000BB">xslt_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">" and the error code is " </span><span style="color: #007700">. </span><span style="color: #0000BB">xslt_errno</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">xslt_free</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> 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 <acronym title="eXtensible Markup Language">XML</acronym> and <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> code from external sources, such as a database or a socket. In these cases you'll have the <acronym title="eXtensible Markup Language">XML</acronym> and/or <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> data in a variable -- and in production applications the overhead of dumping these to file may be too much. This is where <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym>'s "argument" syntax, comes to the rescue. Instead of files as the <acronym title="eXtensible Markup Language">XML</acronym> and <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> arguments to the <span class="function"><strong>xslt_process()</strong></span> function, you can specify "argument place holders" which are then substituted by values given in the arguments array (5th parameter to the <span class="function"><strong>xslt_process()</strong></span> function). The following is an example of processing <acronym title="eXtensible Markup Language">XML</acronym> and <acronym title="eXtensible Stylesheet Language Transformations">XSLT</acronym> into a result variable without the use of files at all. </p> <p class="para"> <div class="example" id="example-5552"> <p><strong>Example #3 Using the <span class="function"><strong>xslt_process()</strong></span> to transform a variable containing <acronym title="eXtensible Markup Language">XML</acronym> data and a variable containing <acronym>XSL</acronym> data into a variable containing the resulting <acronym title="eXtensible Markup Language">XML</acronym> data</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// $xml and $xsl contain the XML and XSL data<br /><br /></span><span style="color: #0000BB">$arguments </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'/_xml' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$xml</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'/_xsl' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$xsl<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Allocate a new XSLT processor<br /></span><span style="color: #0000BB">$xh </span><span style="color: #007700">= </span><span style="color: #0000BB">xslt_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Process the document<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">xslt_process</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">, </span><span style="color: #DD0000">'arg:/_xml'</span><span style="color: #007700">, </span><span style="color: #DD0000">'arg:/_xsl'</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">$arguments</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into the \$result"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" variable, the \$result variable has the following contents\n<br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"<pre>\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">$result</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"</pre>\n"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" the \$result variable the reason is that " </span><span style="color: #007700">. </span><span style="color: #0000BB">xslt_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">" and the error code is " </span><span style="color: #007700">. </span><span style="color: #0000BB">xslt_errno</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">xslt_free</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5553"> <p><strong>Example #4 Passing PHP variables to XSL files</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// XML string<br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= </span><span style="color: #DD0000">'<?xml version="1.0"?><br /><para><br /> change me<br /></para>'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// XSL string<br /></span><span style="color: #0000BB">$xsl </span><span style="color: #007700">= </span><span style="color: #DD0000">'<br /><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><br /><xsl:output method="html" encoding="ISO-8859-1" indent="no"<br /> omit-xml-declaration="yes" media-type="text/html"/><br /> <xsl:param name="myvar"/><br /> <xsl:param name="mynode"/><br /> <xsl:template match="/"><br />My PHP variable : <xsl:value-of select="$myvar"/><br /><br />My node set : <xsl:value-of select="$mynode"/><br /> </xsl:template><br /></xsl:stylesheet>'</span><span style="color: #007700">;<br /><br /><br /></span><span style="color: #0000BB">$xh </span><span style="color: #007700">= </span><span style="color: #0000BB">xslt_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// the second parameter will be interpreted as a string<br /></span><span style="color: #0000BB">$parameters </span><span style="color: #007700">= array (<br /> </span><span style="color: #DD0000">'myvar' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'test'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'mynode' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'<foo>bar</foo>'<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arguments </span><span style="color: #007700">= array (<br /> </span><span style="color: #DD0000">'/_xml' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$xml</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'/_xsl' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$xsl<br /></span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">xslt_process</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">, </span><span style="color: #DD0000">'arg:/_xml'</span><span style="color: #007700">, </span><span style="color: #DD0000">'arg:/_xsl'</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">$arguments</span><span style="color: #007700">, </span><span style="color: #0000BB">$parameters</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> My PHP variable : test<br> My node set : &lt;foo&gt;bar&lt;/foo&gt; </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.xslt-process-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">Please note that <em>file://</em> is needed in front of the path when using Windows. </p></p></blockquote> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.xslt-getopt.html">xslt_getopt</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.xslt-set-base.html">xslt_set_base</a></div> <div class="up"><a href="ref.xslt.html">XSLT (PHP 4) Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>