国产探花免费观看_亚洲丰满少妇自慰呻吟_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"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连山| 宜兰市| 友谊县| 虎林市| 金秀| 大关县| 安远县| 洛川县| 准格尔旗| 乐安县| 镇巴县| 潞西市| 孝昌县| 贡觉县| 临清市| 三穗县| 皮山县| 萍乡市| 北辰区| 通化县| 南漳县| 南通市| 四平市| 大同县| 特克斯县| 襄垣县| 二连浩特市| 榆社县| 子洲县| 吉安市| 界首市| 石首市| 阳西县| 剑川县| 江安县| 胶州市| 晋州市| 金平| 通州区| 元氏县| 绥棱县|