将 PHP XSLT 处理器升级到 XSLT 2.0

2021-12-19 00:00:00 php xslt xslt-2.0 libxslt

升级 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

相关文章