PHP functions in XSLT
The versions of XSLT and XPath provided with PHP are only version 1.0, but includes the facility to directly call PHP functions from XPath statements.
Later versions of XSLT have added many functions, but being able to call PHP functions may more than make up for not being able to use those new functions. The facility does not provide any new functions other than the
Doing complex processing solely in XSLT can make for very complex code, with some functionality being impossible to implement, whereas using PHP functions can provide ad-hoc accessing of files or other XML documents, or just independent processing on the same document that the XSLT is processing, but which would require advanced XSLT understanding, and a brain the size of a planet to keep track of what is going on within it, or asking a lot of questions of Dimitre Novatchev on
A particularly useful use of such PHP functions is inline in the
For functions to be accessible to XSLT, their names must be added to the
To be able to use them in XSLT stylesheets, the http://php.net/xsl
. To call a function from a Xpath statement, use php:function('name',par1,par2)
, where name
is the name of the function, prefixed by class name and ::
if required, and the rest is the parameter values. There can be many parameters, separating each with a comma.[1]
Where care must be taken, and which may have led some to rate PHP functions as unreliable, is in correctly specifying the parameters and return values, with XML attributes being the one that likely creates the most misunderstandings. Firstly, string and numeric literal parameters, like 'ltr'
and 23
respectively, will create no problems as long as they are treated as expected by the PHP code. Likewise, XSLT variables created using them in a
When an XSLT variable with XML elements or an attribute specified by an XPath statement in a
It will be easier if the PHP functions only rely upon receiving elements in this way. Then an empty array will indicate that there are none.
When passing an attribute, make sure it is converted to a string by using the XSLT
When an element is passed to PHP, it is not deep, in that it is like an isolated element that has no parent, but only descendants. This is the same as when passing an element as a parameter in XSLT, whereas making the required element current as the context for the called template does allow traversing the ancestor tree. However, there is no way to pass a context to PHP in the same way. So if wanting PHP to process an element's ancestors, pass PHP the information it requires to independently find the element in the XML structure of the source file.
On the return side, a literal or variable that is a string, number or boolean value will be treated as such in the XSLT. The only way I found to return elements was within a single DOM element. Typically, I created the structure in SimpleXML and returned the element converted by
This could be used directly as the root element in the XPath of an
XSLT variables cannot be modified, except that one defined within an
This can be used to increment counters that can be used in element
While I have observed that attributes in PHP's XSLT have always been processed in order, the XML specification does not require it, so the observed behaviour may change, even though there has been no attempt to bring XML, XPath and XSLT in PHP up to date for many years. To mitigate against future changes to that, iterate over a list of elements that match the attribute names in the order required, and use that to access the attribute values.
If the PHP function always returns a value, and wanting to use the value within XSLT without the returned value appearing in the XSLT rendered output, use it in the
Following these basic guidelines should provide reliable bidirectional dynamic inter-operability between PHP and the XSLT processor.
The PHP