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">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;Allocate&nbsp;a&nbsp;new&nbsp;XSLT&nbsp;processor<br /></span><span style="color: #0000BB">$xh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xslt_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Process&nbsp;the&nbsp;document<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">xslt_process</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'sample.xml'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'sample.xsl'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'result.xml'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"SUCCESS,&nbsp;sample.xml&nbsp;was&nbsp;transformed&nbsp;by&nbsp;sample.xsl&nbsp;into&nbsp;result.xml"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">",&nbsp;result.xml&nbsp;has&nbsp;the&nbsp;following&nbsp;contents\n&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;pre&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">'result.xml'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;/pre&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Sorry,&nbsp;sample.xml&nbsp;could&nbsp;not&nbsp;be&nbsp;transformed&nbsp;by&nbsp;sample.xsl&nbsp;into"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;result.xml&nbsp;the&nbsp;reason&nbsp;is&nbsp;that&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">xslt_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;and&nbsp;the&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"error&nbsp;code&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</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">?&gt;</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">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;Allocate&nbsp;a&nbsp;new&nbsp;XSLT&nbsp;processor<br /></span><span style="color: #0000BB">$xh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xslt_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Process&nbsp;the&nbsp;document,&nbsp;returning&nbsp;the&nbsp;result&nbsp;into&nbsp;the&nbsp;$result&nbsp;variable<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xslt_process</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'sample.xml'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'sample.xsl'</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"SUCCESS,&nbsp;sample.xml&nbsp;was&nbsp;transformed&nbsp;by&nbsp;sample.xsl&nbsp;into&nbsp;the&nbsp;\$result"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;variable,&nbsp;the&nbsp;\$result&nbsp;variable&nbsp;has&nbsp;the&nbsp;following&nbsp;contents\n&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;pre&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;/pre&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Sorry,&nbsp;sample.xml&nbsp;could&nbsp;not&nbsp;be&nbsp;transformed&nbsp;by&nbsp;sample.xsl&nbsp;into"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;the&nbsp;\$result&nbsp;variable&nbsp;the&nbsp;reason&nbsp;is&nbsp;that&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">xslt_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;and&nbsp;the&nbsp;error&nbsp;code&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</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">?&gt;</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&#039;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&#039;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&#039;s &quot;argument&quot; syntax, comes to the   rescue.  Instead of files as the XML and XSLT arguments to the <b>xslt_process()</b>   function, you can specify &quot;argument place holders&quot; 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">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;$xml&nbsp;and&nbsp;$xsl&nbsp;contain&nbsp;the&nbsp;XML&nbsp;and&nbsp;XSL&nbsp;data<br /><br /></span><span style="color: #0000BB">$arguments&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'/_xml'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'/_xsl'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$xsl<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Allocate&nbsp;a&nbsp;new&nbsp;XSLT&nbsp;processor<br /></span><span style="color: #0000BB">$xh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xslt_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Process&nbsp;the&nbsp;document<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xslt_process</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'arg:/_xml'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'arg:/_xsl'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arguments</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"SUCCESS,&nbsp;sample.xml&nbsp;was&nbsp;transformed&nbsp;by&nbsp;sample.xsl&nbsp;into&nbsp;the&nbsp;\$result"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;variable,&nbsp;the&nbsp;\$result&nbsp;variable&nbsp;has&nbsp;the&nbsp;following&nbsp;contents\n&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;pre&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;/pre&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Sorry,&nbsp;sample.xml&nbsp;could&nbsp;not&nbsp;be&nbsp;transformed&nbsp;by&nbsp;sample.xsl&nbsp;into"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;the&nbsp;\$result&nbsp;variable&nbsp;the&nbsp;reason&nbsp;is&nbsp;that&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">xslt_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;and&nbsp;the&nbsp;error&nbsp;code&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</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">?&gt;</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">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;XML&nbsp;string<br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'&lt;?xml&nbsp;version="1.0"?&gt;<br />&lt;para&gt;<br />&nbsp;change&nbsp;me<br />&lt;/para&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;XSL&nbsp;string<br /></span><span style="color: #0000BB">$xsl&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'<br />&lt;xsl:stylesheet&nbsp;version="1.0"&nbsp;xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;<br />&lt;xsl:output&nbsp;method="html"&nbsp;encoding="ISO-8859-1"&nbsp;indent="no"<br />&nbsp;omit-xml-declaration="yes"&nbsp;&nbsp;media-type="text/html"/&gt;<br />&nbsp;&lt;xsl:param&nbsp;name="myvar"/&gt;<br />&nbsp;&lt;xsl:param&nbsp;name="mynode"/&gt;<br />&nbsp;&lt;xsl:template&nbsp;match="/"&gt;<br />My&nbsp;PHP&nbsp;variable&nbsp;:&nbsp;&lt;xsl:value-of&nbsp;select="$myvar"/&gt;&lt;br&nbsp;/&gt;<br />My&nbsp;node&nbsp;set&nbsp;:&nbsp;&lt;xsl:value-of&nbsp;select="$mynode"/&gt;<br />&nbsp;&lt;/xsl:template&gt;<br />&lt;/xsl:stylesheet&gt;'</span><span style="color: #007700">;<br /><br /><br /></span><span style="color: #0000BB">$xh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xslt_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;second&nbsp;parameter&nbsp;will&nbsp;be&nbsp;interpreted&nbsp;as&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$parameters&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'myvar'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'mynode'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'&lt;foo&gt;bar&lt;/foo&gt;'<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arguments&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'/_xml'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'/_xsl'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$xsl<br /></span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">xslt_process</span><span style="color: #007700">(</span><span style="color: #0000BB">$xh</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'arg:/_xml'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'arg:/_xsl'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arguments</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$parameters</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</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&lt;br&gt;My node set : &amp;lt;foo&amp;gt;bar&amp;lt;/foo&amp;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 + -
显示快捷键?