問題描述
升級 PHP 庫以使用 XSLT 2.0 是否可能/容易?
當前設置:
xsl啟用 XSLlibxslt 版本 1.1.24libxslt 針對 libxml 版本 2.6.32 編譯啟用 EXSLTlibexslt 版本 1.1.24
Saxon-C 項目為其 XSLT 2.0 實現提供了一個 PHP API.
這里是基本的安裝過程:
<塊引用>請在您的機器上安裝以下軟件包來構建 Saxon/C PHP 擴展:make、php-devel、(php5-dev/php55-dev/php55w-devel)、apache2 或 httpd、gcc-c++ 或 g++,gcj(或只是鏈接 jni.h 文件)
運行命令:
phpize./configure --enable-saxon制作須藤制作安裝
<塊引用>
更新 php.ini 文件(如果使用 Ubuntu,它通常位于/etc/php5/apache2/"位置)以包含 php 擴展名.在 Dynamic Extensions 部分插入以下內容:extension=saxon.so
運行命令:
sudo service apache2 restart
示例代碼:
setSourceFile($xmlfile);$proc->setStylesheetFile($xslFile);$result = $proc->transformToString();if($result != null) {echo '<b/>exampleSimple1:</b/><br/>';echo '輸出:'.$result;} 別的 {echo "結果為空";}$proc->clearParameters();$proc->clearProperties();}$foo_xml = "xml/foo.xml";$foo_xsl = "xsl/foo.xsl";$proc = new SaxonProcessor();//在 Windows 上,我們建議使用重載的構造函數設置 cwd//因為在使用函數 VCWD_GETCWD 時用 PHP 構建 Saxon/C 仍然存在問題.即 $proc = new SaxonProcessor('C://www/html//trax//');$version = $proc->version();echo '撒克遜處理器版本:'.$version;回聲'<br/>';exampleSimple1($proc, $foo_xml, $foo_xsl);?>
libxslt2 和 libexslt 庫僅限于 XSLT 1.0、XPath 1.0 和 EXSLT 支持,用于為 PHP 提供默認的 XSLT 處理器.XML_XSLT2Processor 項目旨在提供升級路徑.
這里是基本的安裝過程:
<塊引用>按照您要使用的處理器站點上提供的說明獲取有關如何安裝該 XSLT 處理器的說明.基本上,您需要在某個目錄中提取處理器二進制文件.
設置處理器后,您可以下載 XML_XSLT2Processor.使用 PEAR 安裝程序
如果您還沒有 PEAR 安裝程序,請查看 PEAR 站點上的安裝說明(基本上,在 Windows 上,您啟動 PHP 文件夾中的 go-pear.bat 文件,在典型情況下單擊Enter"一路),并安裝 PEAR 安裝程序,又名PEAR 包管理器".
一旦你有了 PEAR 安裝程序,你就可以從它安裝 XML_XSLT2Processor,只需輸入梨安裝路徑/to/the/tgz/arhive但當然要更換路徑.例如,如果 0.5.3 版本與 PHP 文件夾在同一文件夾中,則可以使用以下命令安裝它梨安裝 XML_XSLT2Processor_v0_5_3.tgz
手動安裝
<塊引用>如果您沒有(訪問)PEAR 安裝程序,您仍然可以通過在任何目錄中提取存檔內容來安裝 XML_XSLT2Processor.但是,建議將此目錄放在您的 include_path 中的路徑中,您可以在 php.ini 中指定該路徑.為了更接近地模擬 PEAR 安裝程序,您還可以將XSLT2Processor-verion"目錄重命名為XML".
用法
<塊引用>完成上述所有操作后,您可以創建一個新的 PHP 文件并在其中包含 XML_XSLT2Processor.如果您使用過 PEAR 安裝程序,XSLT2Processor.php"應該可以從XML"文件夾中找到,因此:
<塊引用>
您需要在 PHP 文件中包含將使用該類的包含行,它應該在您使用該類中的任何函數之前出現.文檔的其余部分將向您展示如何構造 XML_XSLT2Processor 類,并解釋每個函數的原型并給出一些示例.
請注意,如果您在使用這個擴展之前使用過 PHP XSL 擴展,那么您真正必須知道的唯一一件事就是 XML_XSLT2Processor::__construct() 函數.其余的與它兼容,盡管有一些新功能僅在此處可用.請注意 registerPHPFunctions() 和 setProfiling() 函數由于類的架構(不是 PECL 擴展和所有...)而無法使用.
參考資料
安裝 XSLT 處理器
XSLT2Processor 項目新聞
Saxon-C 產品信息
Saxon-C PHP API
Is it possible/easy to upgrade PHP's library to use XSLT 2.0?
Current set up:
xsl
XSL enabled
libxslt Version 1.1.24
libxslt compiled against libxml Version 2.6.32
EXSLT enabled
libexslt Version 1.1.24
The Saxon-C project provides a PHP API for its XSLT 2.0 implementation.
Here is the basic installation process:
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
Example code:
<?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);
?>
The libxslt2 and libexslt libraries, which are limited to XSLT 1.0, XPath 1.0, and EXSLT support, are used to provide the default XSLT processor for PHP. The XML_XSLT2Processor project is intended to provide an upgrade path.
Here is the basic installation process:
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
Manual installation
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".
Usage
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...).
References
Installing an XSLT Processor
XSLT2Processor Project News
Saxon-C Product Information
Saxon-C PHP API
這篇關于將 PHP XSLT 處理器升級到 XSLT 2.0的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!