function.xslt-process.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 300 行 · 第 1/2 页
HTML
300 行
</div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. If the result container is not specified - i.e. <b><tt>NULL</tt></b> - than the result is returned. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.0.6</td> <td colspan="1" rowspan="1" align="left"> This function no longer takes XML strings in <i><tt class="parameter">xmlcontainer</tt></i> or <i><tt class="parameter">xslcontainer</tt></i>. 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> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> The simplest type of transformation with the <b>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> <div class="example"> <p><b>Example #1 Using the <b>xslt_process()</b> to transform an XML file and a XSL file to a new XML file</b></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 <b>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 class="para"> <div class="example"> <p><b>Example #2 Using the <b>xslt_process()</b> to transform an XML file and a XSL file to a variable containing the resulting XML data</b></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 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>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>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 class="para"> <div class="example"> <p><b>Example #3 Using the <b>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> <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"> <p><b>Example #4 Passing PHP variables to XSL files</b></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"><pre><div class="cdata"><pre>My PHP variable : test<br>My node set : &lt;foo&gt;bar&lt;/foo&gt;</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: Please note that<i>file://</i> is needed in front of the path when using Windows.<br /></p></blockquote> </div></div><hr /><div 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 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?