php专区

 首页 > php专区 > PHP应用 > 常用功能 > XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数

XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数

分享到:
【字体:
导读:
          XSLT 是一个非常方便的转换 XML 的工具,PHP 里面是通过 XSLTProcessor 来实现;XSLT 中内置了许多有用的函数,同时,只需要调用 XSLTProcessor 实例的 registerPHPFunctions 方法...

XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数

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. $xml = <<
  2.   
  3.    
  4.   bob  
  5.    
  6.    
  7.   joe  
  8.    
  9.   
  10. EOB;  
  11. $xsl = <<
  12. "1.0" encoding="UTF-8"?>  
  13. "1.0" 
  14.      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  15. "html" encoding="utf-8" indent="yes"/>  
  16.  "allusers">  
  17.     
  18.     

    Users

      
  19.       
  20.     for-each select="user">  
  21.       
  22.   
  23.     for-each>  
  24.     
  25.   
  26.         
  27.              select="php:function('ucfirst',string(uid))"/>  
  28.       
  29.   
  30.     
  31.    
  32.   
  33. EOB;  
  34. $xmldoc = DOMDocument::loadXML($xml);  
  35. $xsldoc = DOMDocument::loadXML($xsl);  
  36.    
  37. $proc = new XSLTProcessor();  
  38. $proc->registerPHPFunctions();  
  39. $proc->importStyleSheet($xsldoc);  
  40. echo $proc->transformToXML($xmldoc);  
  41. ?> 

其实,出现这种错误,是因为我们没有定义 PHP namespace,只需要在

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

中增加 xmlns:php="http://php.net/xsl" 就能解决此问题, 即

  1. "1.0" 
  2.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  3.     xmlns:php="http://php.net/xsl"
分享到:
php使用dom解析含有中文字符xml文档 - ph...
php使用dom解析含有中文字符xml文档 一般我们直接使用dom来处理xml文档时如果里面有中文就会把中文转换成乱码了,下面我们通过使用iconv()函数实现编码转换,防止中文乱码.代码如下:   xml文件代码如下:          2012      元旦      2012-1-1...
CentOS系统下Apache环境安装GBK编码程序...
CentOS系统下Apache环境安装GBK编码程序网页乱码问题 问题描述 在CentOS系统中采用Apache服务器安装GBK编码的程序出现乱码,这主要是因为默认安装的Apache服务器字符编码为UTF-8,如果GBK程序中没有强制编码将产生乱码。 解决方法: 1,打开Apache配置文件httpd.conf,如默认情况下为 /etc...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……