将 PHP XSLT 处理器升级到 XSLT 2.0

发布于 2024-09-26 12:20:40 字数 218 浏览 0 评论 0原文

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

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2024-10-03 12:20:40

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
make
sudo make install

更新 php.ini 文件(如果使用 Ubuntu,它通常位于“/etc/php5/apache2/”位置)以包含 php 扩展。在动态扩展部分中插入以下内容:extension=saxon.so

运行命令:

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。 XML_XSLT2Processor 项目旨在提供升级路径。

以下是基本安装过程:

按照您要使用的处理器网站上提供的说明来获取有关如何安装该 XSLT 处理器的说明。基本上,您需要将处理器二进制文件提取到某个目录中。

设置处理器后,您可以下载 XML_XSLT2Processor。
使用 PEAR 安装程序

如果您还没有 PEAR 安装程序,请检查 PEAR 站点上的安装说明(基本上,在 Windows 上,您启动 PHP 文件夹中的 go-pear.bat 文件,然后在典型情况下单击“Enter”一路),然后安装 PEAR 安装程序(又名“PEAR 包管理器”)。

一旦你有了 PEAR 安装程序,你就可以通过它安装 XML_XSLT2Processor,只需输入
pear 安装路径/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 
include "XML/XSLT2Processor.php";
//The rest of the code
?>

您需要在将使用该类的 PHP 文件中包含包含行,并且它应该在使用该类中的任何函数之前出现。文档的其余部分将向您展示如何构造 XML_XSLT2Processor 类,并解释每个函数的原型并提供一些示例。

请注意,如果您在使用此扩展之前已经使用过 PHP XSL 扩展,那么您真正必须了解的唯一一件事就是 XML_XSLT2Processor::__construct() 函数。其余部分与其兼容,尽管有一些新功能仅在此处可用。请注意,由于类的体系结构(不是 PECL 扩展等),registerPHPFunctions() 和 setProfiling() 函数不可用。

参考

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文