升级 PHP 库以使用 XSLT 2.0 是否可能/容易?
当前设置:
xsl
XSL enabled
libxslt Version 1.1.24
libxslt compiled against libxml Version 2.6.32
EXSLT enabled
libexslt Version 1.1.24
最佳答案
Saxon-C项目为其 XSLT 2.0 实现提供了一个 PHP API。
基本安装过程如下:
Please have the following packages on your machine to build the Saxon/C PHP extension: make, php-devel, (php5-dev/php55-dev/php55w-devel), apache2 or httpd, gcc-c++ or g++, gcj (or just link the jni.h file)
Run the commands:
phpize
./configure --enable-saxon
make
sudo make install
Update the php.ini file (if using Ubuntu it is usually in the location '/etc/php5/apache2/') to contain the php extension. Insert the following in the Dynamic Extensions section: extension=saxon.so
Run the command:
sudo service apache2 restart
示例代码:
<?php
/* simple example to show transforming to string */
function exampleSimple1($proc, $xmlfile, $xslFile){
$proc->setSourceFile($xmlfile);
$proc->setStylesheetFile($xslFile);
$result = $proc->transformToString();
if($result != null) {
echo '<b/>exampleSimple1:</b/><br/>';
echo 'Output:'.$result;
} else {
echo "Result is null";
}
$proc->clearParameters();
$proc->clearProperties();
}
$foo_xml = "xml/foo.xml";
$foo_xsl = "xsl/foo.xsl";
$proc = new SaxonProcessor();
//On Windows we recommend setting the cwd using the overloaded constructor
//because there remains an issue with building Saxon/C with PHP when using the function VCWD_GETCWD. i.e. $proc = new SaxonProcessor('C://www/html//trax//');
$version = $proc->version();
echo 'Saxon Processor version: '.$version;
echo '<br/>';
exampleSimple1($proc, $foo_xml, $foo_xsl);
?>
libxslt2 和 libexslt 库仅限于 XSLT 1.0、XPath 1.0 和 EXSLT 支持,用于为 PHP 提供默认的 XSLT 处理器。 XML_XSLT2Processor项目旨在提供升级路径。
基本安装过程如下:
Follow the instructions provided on the site of the processor you want to use for instructions on how to install that XSLT processor. Basically, you'll be required to extract the processor binary in some directory.
Once you have the processor set up, you can download XML_XSLT2Processor. Using the PEAR installer
If you don't already have the PEAR installer, check the installation instructions on the PEAR site (basically, on Windows, you start the go-pear.bat file in PHP's folder, and in the typical case click "Enter" all the way), and install the PEAR installer a.k.a. the "PEAR package manager".
Once you have the PEAR installer, you can install XML_XSLT2Processor from it, by simply typing pear install path/to/the/tgz/arhive but replace the path of course. For example, if version 0.5.3 was in the same folder as the PHP folder, you can install it with the command pear install XML_XSLT2Processor_v0_5_3.tgz
手动安装
If you don't have (access to) the PEAR installer, you can still install XML_XSLT2Processor by extracting the contents of the archive in any directory. However, it is recommended that this directory is among the paths in your include_path, which you can specify in php.ini. To more closely emulate the PEAR installer, you may also rename the "XSLT2Processor-verion" directory to "XML".
用法
Once all of the above is done, you can create a new PHP file and include XML_XSLT2Processor in it. If you've used the PEAR installer, "XSLT2Processor.php" should be available from the "XML" folder, thus:
<?php
include "XML/XSLT2Processor.php";
//The rest of the code
?>
You'll need the include line in the PHP file that will be using the class and it should occur before you use any of the functions in that class. The rest of the documentation will show you how to construct the XML_XSLT2Processor class, as well as explain each function's prototype and give some examples.
Note that if you've worked with the PHP XSL extension before using this one, the only thing you really must know is the XML_XSLT2Processor::__construct() function. The rest is compatible with it, though there are some new features available only here. Be aware that the registerPHPFunctions() and setProfiling() functions are not available due to the architecture of the class (not being a PECL extension and all...).
引用资料
关于php - 将 PHP XSLT 处理器升级到 XSLT 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3873996/