国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

XSLTProcessor 中 registerPHPFunctions 后無法調用 php 函數

2024-05-04 21:47:19
字體:
來源:轉載
供稿:網友

XSLT 是一個非常方便的轉換 XML 的工具,PHP 里面是通過 XSLTProcessor 來實現;XSLT 中內置了許多有用的函數,同時,只需要調用 XSLTProcessor 實例的 registerPHPFunctions 方法,我們就可以在 XSLT 中直接使用 PHP 的函數,這大大增強了 XSLT 的處理能力。

但是,在 XSLT 中使用 PHP 函數時,很多人會遇到如下兩種錯誤:

(1)、Warning: XSLTProcessor::transformToXml(): xmlXPathCompiledEval: 1 objects left on the stack.

(2)、PHP Warning: XSLTProcessor::transformToXml(): xmlXPathCompOpEval: function function bound to undefined prefix php in ….

  1. <?php  
  2. $xml = <<<EOB  
  3. <allusers>  
  4.  <user>  
  5.   <uid>bob</uid>  
  6.  </user>  
  7.  <user>  
  8.   <uid>joe</uid>  
  9.  </user>  
  10. </allusers>  
  11. EOB;  
  12. $xsl = <<<EOB  
  13. <?xml version="1.0" encoding="UTF-8"?>  
  14. <xsl:stylesheet version="1.0" 
  15.      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  16. <xsl:output method="html" encoding="utf-8" indent="yes"/>  
  17.  <xsl:template match="allusers">  
  18.   <html><body>  
  19.     <h2>Users</h2>  
  20.     <table>  
  21.     <xsl:for-each select="user">  
  22.       <tr><td>  
  23.         <xsl:value-of  
  24.              select="php:function('ucfirst',string(uid))"/>  
  25.       </td></tr>  
  26.     </xsl:for-each>  
  27.     </table>  
  28.   </body></html>  
  29.  </xsl:template>  
  30. </xsl:stylesheet>  
  31. EOB;  
  32. $xmldoc = DOMDocument::loadXML($xml);  
  33. $xsldoc = DOMDocument::loadXML($xsl);  
  34.    
  35. $proc = new XSLTProcessor();  
  36. $proc->registerPHPFunctions();  
  37. $proc->importStyleSheet($xsldoc);  
  38. echo $proc->transformToXML($xmldoc);  
  39. ?> 

其實,出現這種錯誤,是因為我們沒有定義 PHP namespace,只需要在

  1. <xsl:stylesheet version="1.0" 
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

中增加 xmlns:php="http://php.net/xsl" 就能解決此問題, 即

  1. <xsl:stylesheet version="1.0" 
  2.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  3.     xmlns:php="http://php.net/xsl"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上犹县| 吴桥县| 紫阳县| 洱源县| 营口市| 临漳县| 兰西县| 蓬溪县| 手游| 锡林浩特市| 柳林县| 武穴市| 房山区| 西青区| 武清区| 陕西省| 滦南县| 枝江市| 康保县| 德兴市| 久治县| 绥棱县| 土默特左旗| 洪雅县| 凭祥市| 汤原县| 宣恩县| 江安县| 英德市| 独山县| 遵化市| 华宁县| 鹤岗市| 葫芦岛市| 尚志市| 新巴尔虎左旗| 阿克苏市| 平顺县| 闽清县| 正安县| 晋宁县|