<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >xslt_process</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="XSLT Functions" HREF="ref.xslt.html"><LINK REL="PREVIOUS" TITLE="xslt_getopt" HREF="function.xslt-getopt.html"><LINK REL="NEXT" TITLE="xslt_set_base" HREF="function.xslt-set-base.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.xslt-getopt.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.xslt-set-base.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.xslt-process" ></A >xslt_process</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN124750" ></A ><P > (PHP 4 >= 4.0.3)</P >xslt_process -- Perform an XSLT transformation</DIV ><DIV CLASS="refsect1" ><A NAME="AEN124753" ></A ><H2 >Description</H2 >mixed <B CLASS="methodname" >xslt_process</B > ( resource xh, string xmlcontainer, string xslcontainer [, string resultcontainer [, array arguments [, array parameters]]])<BR ></BR ><P > The <B CLASS="function" >xslt_process()</B > function is the crux of the new 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. The result container defaults to a filename for the transformed document. If the result container is not specified - i.e. <TT CLASS="constant" ><B >NULL</B ></TT > - than the result is returned. </P ><P > <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Avertisment</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > This function has changed its arguments, sinceversion 4.0.6. Do NOT provide the actual XML or XSL content as 2nd and 3rd argument, as this will create a segmentation fault, in Sablotron versions up to and including 0.95. </P ></TD ></TR ></TABLE ></DIV > </P ><P > Containers can also be set via the <VAR CLASS="parameter" >$arguments</VAR > array (see below). </P ><P > The simplest type of transformation with the <B CLASS="function" >xslt_process()</B > 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... </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN124786" ></A ><P ><B >Exemplu 1. Using the <B CLASS="function" >xslt_process()</B > to transform an XML file and a XSL file to a new XML file</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br /></font><font color="#FF8000">// Allocate a new XSLT processor<br /></font><font color="#0000BB">$xh </font><font color="#007700">= </font><font color="#0000BB">xslt_create</font><font color="#007700">();<br /><br /></font><font color="#FF8000">// Process the document<br /></font><font color="#007700">if (</font><font color="#0000BB">xslt_process</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">, </font><font color="#DD0000">'sample.xml'</font><font color="#007700">, </font><font color="#DD0000">'sample.xsl'</font><font color="#007700">, </font><font color="#DD0000">'result.xml'</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into result.xml"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">", result.xml has the following contents\n<br />\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"<pre>\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">readfile</font><font color="#007700">(</font><font color="#DD0000">'result.xml'</font><font color="#007700">);<br /> echo </font><font color="#DD0000">"</pre>\n"</font><font color="#007700">;<br />} else {<br /> echo </font><font color="#DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">" result.xml the reason is that " </font><font color="#007700">. </font><font color="#0000BB">xslt_error</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">) . </font><font color="#DD0000">" and the "</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"error code is " </font><font color="#007700">. </font><font color="#0000BB">xslt_errno</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">xslt_free</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > 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 <B CLASS="function" >xslt_process()</B > 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... </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN124793" ></A ><P ><B >Exemplu 2. Using the <B CLASS="function" >xslt_process()</B > to transform an XML file and a XSL file to a variable containing the resulting XML data</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br /></font><font color="#FF8000">// Allocate a new XSLT processor<br /></font><font color="#0000BB">$xh </font><font color="#007700">= </font><font color="#0000BB">xslt_create</font><font color="#007700">();<br /><br /></font><font color="#FF8000">// Process the document, returning the result into the $result variable<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">xslt_process</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">, </font><font color="#DD0000">'sample.xml'</font><font color="#007700">, </font><font color="#DD0000">'sample.xsl'</font><font color="#007700">);<br />if (</font><font color="#0000BB">$result</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into the \$result"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">" variable, the \$result variable has the following contents\n<br />\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"<pre>\n"</font><font color="#007700">;<br /> echo </font><font color="#0000BB">$result</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"</pre>\n"</font><font color="#007700">;<br />} else {<br /> echo </font><font color="#DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">" the \$result variable the reason is that " </font><font color="#007700">. </font><font color="#0000BB">xslt_error</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">); <br /> echo </font><font color="#DD0000">" and the error code is " </font><font color="#007700">. </font><font color="#0000BB">xslt_errno</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">xslt_free</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 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 <B CLASS="function" >xslt_process()</B > function, you can specify "argument place holders" which are then substituted by values given in the arguments array (5th parameter to the <B CLASS="function" >xslt_process()</B > function). The following is an example of processing XML and XSLT into a result variable without the use of files at all. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN124801" ></A ><P ><B >Exemplu 3. Using the <B CLASS="function" >xslt_process()</B > to transform a variable containing XML data and a variable containing XSL data into a variable containing the resulting XML data</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// $xml and $xsl contain the XML and XSL data<br /><br /></font><font color="#0000BB">$arguments </font><font color="#007700">= array(<br /> </font><font color="#DD0000">'/_xml' </font><font color="#007700">=> </font><font color="#0000BB">$xml</font><font color="#007700">,<br /> </font><font color="#DD0000">'/_xsl' </font><font color="#007700">=> </font><font color="#0000BB">$xsl<br /></font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Allocate a new XSLT processor<br /></font><font color="#0000BB">$xh </font><font color="#007700">= </font><font color="#0000BB">xslt_create</font><font color="#007700">();<br /><br /></font><font color="#FF8000">// Process the document<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">xslt_process</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">, </font><font color="#DD0000">'arg:/_xml'</font><font color="#007700">, </font><font color="#DD0000">'arg:/_xsl'</font><font color="#007700">, </font><font color="#0000BB">NULL</font><font color="#007700">, </font><font color="#0000BB">$arguments</font><font color="#007700">); <br />if (</font><font color="#0000BB">$result</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into the \$result"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">" variable, the \$result variable has the following contents\n<br />\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"<pre>\n"</font><font color="#007700">;<br /> echo </font><font color="#0000BB">$result</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"</pre>\n"</font><font color="#007700">;<br />} else {<br /> echo </font><font color="#DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">" the \$result variable the reason is that " </font><font color="#007700">. </font><font color="#0000BB">xslt_error</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">) . <br /> echo </font><font color="#DD0000">" and the error code is " </font><font color="#007700">. </font><font color="#0000BB">xslt_errno</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">);<br />}<br /></font><font color="#0000BB">xslt_free</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Finally, the last argument to the <B CLASS="function" >xslt_process()</B > function represents an array for any top-level parameters that you want to pass 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 <SPAN CLASS="productname" >Sablotron</SPAN > processor. In other words - you cannot pass node-sets as parameters to the XSLT document. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN124809" ></A ><P ><B >Exemplu 4. Passing PHP variables to XSL files</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br /></font><font color="#FF8000">// XML string<br /></font><font color="#0000BB">$xml </font><font color="#007700">= </font><font color="#DD0000">'<?xml version="1.0"?><br /><para><br /> change me<br /></para>'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// XSL string<br /></font><font color="#0000BB">$xsl </font><font color="#007700">= </font><font 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>'</font><font color="#007700">;<br /><br /><br /></font><font color="#0000BB">$xh </font><font color="#007700">= </font><font color="#0000BB">xslt_create</font><font color="#007700">();<br /><br /></font><font color="#FF8000">// the second parameter will be interpreted as a string<br /></font><font color="#0000BB">$parameters </font><font color="#007700">= array (<br /> </font><font color="#DD0000">'myvar' </font><font color="#007700">=> </font><font color="#DD0000">'test'</font><font color="#007700">,<br /> </font><font color="#DD0000">'mynode' </font><font color="#007700">=> </font><font color="#DD0000">'<foo>bar</foo>'<br /></font><font color="#007700">);<br /><br /></font><font color="#0000BB">$arguments </font><font color="#007700">= array (<br /> </font><font color="#DD0000">'/_xml' </font><font color="#007700">=> </font><font color="#0000BB">$xml</font><font color="#007700">,<br /> </font><font color="#DD0000">'/_xsl' </font><font color="#007700">=> </font><font color="#0000BB">$xsl<br /></font><font color="#007700">);<br /><br />echo </font><font color="#0000BB">xslt_process</font><font color="#007700">(</font><font color="#0000BB">$xh</font><font color="#007700">, </font><font color="#DD0000">'arg:/_xml'</font><font color="#007700">, </font><font color="#DD0000">'arg:/_xsl'</font><font color="#007700">, </font><font color="#0000BB">NULL</font><font color="#007700">, </font><font color="#0000BB">$arguments</font><font color="#007700">, </font><font color="#0000BB">$parameters</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > this will produce: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >My PHP variable : test<br> My node set : &lt;foo&gt;bar&lt;/foo&gt;</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B >Please note that <SPAN CLASS="emphasis" ><I CLASS="emphasis" >file://</I ></SPAN > is needed in front of path if you use Windows.</P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.xslt-getopt.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.xslt-set-base.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >xslt_getopt</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.xslt.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >xslt_set_base</TD ></TR ></TABLE ></DIV ></BODY ></HTML >