Sophie

Sophie

distrib > PLD > ac > amd64 > media > dist > by-pkgid > dd8ef74e7a184506d40e4328053fb785 > files > 3609

php-manual-ro-20051028-1.noarch.rpm

<!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 &#62;= 4.0.3)</P
>xslt_process&nbsp;--&nbsp;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
>&#13;     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
>&#13;     <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
>&#13;       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
>&#13;     Containers can also be set via the <VAR
CLASS="parameter"
>$arguments</VAR
>
  array (see below).
    </P
><P
>&#13;     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">&lt;?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 />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into result.xml"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">", result.xml has the following contents\n&lt;br /&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;pre&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">readfile</font><font color="#007700">(</font><font color="#DD0000">'result.xml'</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;/pre&gt;\n"</font><font color="#007700">;<br />} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;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">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     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
>&#13;     <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">&lt;?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 />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into the \$result"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">" variable, the \$result variable has the following contents\n&lt;br /&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;pre&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$result</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;/pre&gt;\n"</font><font color="#007700">;<br />} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;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">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     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
>&#13;     <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">&lt;?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 />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'/_xml' </font><font color="#007700">=&gt; </font><font color="#0000BB">$xml</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'/_xsl' </font><font color="#007700">=&gt; </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 />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"SUCCESS, sample.xml was transformed by sample.xsl into the \$result"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">" variable, the \$result variable has the following contents\n&lt;br /&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;pre&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$result</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;/pre&gt;\n"</font><font color="#007700">;<br />} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Sorry, sample.xml could not be transformed by sample.xsl into"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&nbsp;&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;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">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     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 &#60;xsl:param name="parameter_name"&#62;
     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
>&#13;     <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">&lt;?php<br /><br /></font><font color="#FF8000">// XML string<br /></font><font color="#0000BB">$xml </font><font color="#007700">= </font><font color="#DD0000">'&lt;?xml version="1.0"?&gt;<br />&lt;para&gt;<br /> change me<br />&lt;/para&gt;'</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 />&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;<br />&lt;xsl:output method="html" encoding="ISO-8859-1" indent="no" <br /> omit-xml-declaration="yes"&nbsp;&nbsp;media-type="text/html"/&gt;<br /> &lt;xsl:param name="myvar"/&gt;<br /> &lt;xsl:param name="mynode"/&gt;<br /> &lt;xsl:template match="/"&gt;<br />My PHP variable : &lt;xsl:value-of select="$myvar"/&gt;&lt;br /&gt;<br />My node set : &lt;xsl:value-of select="$mynode"/&gt;<br /> &lt;/xsl:template&gt;<br />&lt;/xsl:stylesheet&gt;'</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 />&nbsp;&nbsp;</font><font color="#DD0000">'myvar' </font><font color="#007700">=&gt; </font><font color="#DD0000">'test'</font><font color="#007700">,<br />&nbsp;&nbsp;</font><font color="#DD0000">'mynode' </font><font color="#007700">=&gt; </font><font color="#DD0000">'&lt;foo&gt;bar&lt;/foo&gt;'<br /></font><font color="#007700">);<br /><br /></font><font color="#0000BB">$arguments </font><font color="#007700">= array (<br />&nbsp;&nbsp;</font><font color="#DD0000">'/_xml' </font><font color="#007700">=&gt; </font><font color="#0000BB">$xml</font><font color="#007700">,<br />&nbsp;&nbsp;</font><font color="#DD0000">'/_xsl' </font><font color="#007700">=&gt; </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">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       this will produce:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>My PHP variable : test&#60;br&#62;
My node set : &#38;lt;foo&#38;gt;bar&#38;lt;/foo&#38;gt;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Not&#227;: </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&#227;</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
>