Home >Backend Development >PHP Tutorial >Solution to the problem that php functions cannot be called after registerPHPFunctions in XSLTProcessor_PHP Tutorial

Solution to the problem that php functions cannot be called after registerPHPFunctions in XSLTProcessor_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:55:32958browse

XSLT is a very convenient tool for converting XML. It is implemented in PHP through XSLTProcessor. There are many useful functions built into XSLT. At the same time, we only need to call the registerPHPFunctions method of the XSLTProcessor instance, and we can use it directly in XSLT. PHP functions, which greatly enhance the processing capabilities of XSLT.

However, when using PHP functions in XSLT, many people will encounter the following two errors:

(1) Warning: XSLTProcessor::transformToXml(): xmlXPathCompiledEval: 1 objects left o
n the stack.
(2) PHP Warning: XSLTProcessor::transformToXml(): xmlXPathCompOpEval: function func
tion bound to undefined prefix php in ….

bob
The code is as follows
 代码如下 复制代码

$xml = <<
 
  bob
 

 
  joe
 


EOB;
$xsl = <<
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 
 
   

Users


   
   
     

   
   

        select="php:function('ucfirst',string(uid))"/>
     

 
 

EOB;
$xmldoc = DOMDocument::loadXML($xml);
$xsldoc = DOMDocument::loadXML($xsl);
 
$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);
?>

Copy code

 代码如下 复制代码
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
$xml = <<
 代码如下 复制代码

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl">

joe EOB; $xsl = << xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Users

                                                                                select="php:function('ucfirst',string(uid))"/>
EOB; $xmldoc = DOMDocument::loadXML($xml); $xsldoc = DOMDocument::loadXML($xsl); $proc = new XSLTProcessor(); $proc->registerPHPFunctions(); $proc->importStyleSheet($xsldoc); echo $proc->transformToXML($xmldoc); ?> Actually, this error occurs because we have not defined PHP namespace, we only need to add it in
The code is as follows Copy code
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> This problem can be solved by adding xmlns:php="http://php.net/xsl" in , that is,
The code is as follows Copy code
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"<🎜> xmlns:php="http://php.net/xsl">

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632243.htmlTechArticleXSLT is a very convenient tool for converting XML. It is implemented in PHP through XSLTProcessor; XSLT has many built-in Useful functions, at the same time, only need to call the XSLTProcessor instance...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn