Will Xpath 2.0 And/Or Xslt 2.0 Be Implemented in PHP

Will XPath 2.0 and/or XSLT 2.0 be implemented in PHP?

See this list of XSLT engines, from a thread on an XSL mailing list regarding XSLT 2.0 development.

A solution for PHP is to use the PHP/Java Bridge and Saxon, and follow a tutorial.

The current status of development for XSLT 2.0 (and XPath 2.0) means that there are no plans currently, so there can't be any for PHP either. To use XPath 2.0, you need to include Saxon 9.x and inter-operate with the Java libraries.

Even if someone wrote an open source native library for XPath 2.0 and XSLT 2.0 today, it would take time before it would be suitable for production.

What is the best way to use XSLT 2.0 with PHP?

The question is timely because last week we launched Saxon/C, a port of Saxon compiled to Intel machine code with APIs for C, C++, and PHP. It's early days yet (an Alpha 0.1 release), but if you don't mind being at the bleeding edge, you might give it a try. And of course we welcome your feedback. Details at http://www.saxonica.com/saxon-c/index.xml

XSLT 2.0 PHP support. When?

In the nearest future? None.

PHP5 relies exclusively on LibXSLT for XSLT processing (Sablotron is out), and there is no plan to support XSLT 2.0 in this library.

Upgrade PHP XSLT processor to XSLT 2.0

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

How do I update the version of xpath in PHP?

Unfortunately no-one has upgraded the PHP XPath library to version 2.0. If you have lots of spare time or money, you would to do the community a great service by undertaking this work. If you haven't got lots of spare time or money, then hopefully you will understand why no-one else has taken it on. Sadly, this is the downside to free software; there's often no investment going into it.

XPath 2.0 & LibXML2

Steve Ball bravely started a project to add 2.0 support to libxml early in 2011, but quickly found that it was a bigger project than he had time for, and as far as I can tell, he's made no progress.

format timezone using XSLT/xpath 2.0

A workaround for your problem could be splitting the output of format-dateTime into two parts and remove the colon on the second expression:

concat(format-dateTime(current-dateTime(), "[Y0001]-[M01]-[D01]-[H01]:[m01]:[s]"),translate(format-dateTime(current-dateTime(), "[Z0001]"),":",""))

Maybe this works for you.

Idiomatic way to express set equality in XPath 2.0

Firstly, it depends on how you assess equality between items, for example do you compare two items using "=", "is", "eq", or "deep-equal"? Your own answer suggests that you're thinking of "=", which is almost the same as "eq" when applied to items, except that it has slightly different conversion rules. And as it happens, it's not a good operator to use with sets, because it's not transitive: untypedAtomic("4") = 4, untypedAtomic("4") = "4", but not(4 = "4"). So let's assume "eq", which is transitive except in corner cases involving numeric rounding of values that are "almost equal".

I'd then be inclined to suggest (as others have done)

deep-equal(sort($A), sort($B)) 

except that some data types (e.g. QNames) have an equality operator defined, but no ordering defined. So this will work for integers and strings, but not for QNames.

Obviously there's the O(n^2) method given in your "impulse" solution, but can one do better?

How about

let $A := count(distinct-values($a))
let $B := count(distinct-values($b))
let $AB := count(distinct-values(($a, $b))
return $A = $AB and $B = $AB

This should be O(n log n).

Does libxml2 support XPath 2.0 or not?

As far as I know -- not. The prevailing majority of XPath 2.0 implementations are part of XSLT 2.0 processors or XQuery processors.

Java XSLT processors supporting XPath 2.0

XSLT 2.0 Processors

There are several XSLT 2.0 processors for various languages.

Java

Written for Java, or reported to have a Java interface:

  • Saxon 9.x by Michael Kay
  • WebSphere 7 XML Feature Pack by IBM
  • AltovaXML2009.exe by Altova

.NET

Written for the .NET framework:

  • XQSharp 2.0 by Clinical & Biomedical Computing Ltd.

Eiffel

Written in Eiffel:

  • Gestalt by Colin-Paul Adams

Other

  • SOA Expressway by Intel


Related Topics



Leave a reply



Submit