# PHP Programming/XSL/registerPHPFunctions

The XSLTProcessor::registerPHPFunctions method enables to use PHP (v5.0.4+) functions as XSLT-v1 functions. Is a XSLT/registerFunction facility for "XSLT parser called by PHP".
It is a XSLTProcessor feature for exposing PHP functions or methods to the XSLT script (processed by importStyleSheet method). Very important for PHP users, because PHP (and any libxml2-dependent) not have a XSLT-v2 engine, and part of this functional lack can be overcome by using registerPHPFunctions.
But, even in 2013's, most programmers share of the opinion that
   ... Is poorly documented and poorly supported, and has much ugliness about it. Rely on it as little as possible...
   – expressed by F. Avila
The objective of this chapter, a tutorial for use PHP functions with XSLT, is trying to change this "state of affairs".
NOTE: another functional complement is to use PHP support for EXSLT library (see http://www.exslt.org/). See also , , ... and other tips (not to be confused with libraries of functions with similar names). The Common Module is the most important to use with registerPHPFunctions, having full implementation in all XML parsers.

## Preparing

The XSLTProcessor call need some inicializations, so, we can encapsulate this initializations in a single function, that use XML data and a XSLT script as inputs, and print the XSLTProcessor result.
For send a XSLT script to this XSL_transf function, the XSLT script must be a string, so we can use a inline declaration (see PHP's Nowdoc and Heredoc),
Another way is get it from a file,
or even changing the XSL_transf,

### Cautions

After <xsl:stylesheet version="1.0" ...> declaration you can change the default output by, p. example,
In the examples of this tutorial, use always the XML method,
and, for see all tags withou need to open source-code in the browser, start the PHP script with

## Using PHP functions with static XSLT

In a first overview of the "exposing PHP to XSLT" feature, we can ignore the XML input data, using the XSLT script as a static template.

### XSL receiving external string values

Declare and use of a XSLT script with PHP function calls (see xsl:value-of), that import string values from PHP functions (direct or parametrized).
The first two functions can be called without any parameter, the two last functions are user-declared:
XSL_transf RESULT:

### XSL receiving external XML as string

The <xsl:value-of ... /> clause usually receives string values, but with the disable-output-escaping attribute, it can receive an entire XML fragment.
where
XSL_transf RESULT:

### XSL receiving external XML as DOMElement

All XSLTProcessor activities relies in DOMDocument manipulations, so, for best performance, it is better to send a DOMElement object instead of a string.
The clause <xsl:copy-of ... /> receives DOMElement or DOMDocument, and <xsl:for-each ...> receives DOMNodeList. So, if we have a PHP function that returns DOMDocument, we can use it.
Calling xsl_myF4 into the XSLT script,
RESULTS:

### XSL receiving external fragments

A common need is to handle DOM fragments, that is, a XML without root. In the exemple above, function xsl_myF4_DOMConstant we used <t> foo <tt val="123"/> bar </t>. If the return value of needle is only foo <tt val="123"/> bar the function must be changed to,
but now, to call xsl_myF4b (into the XSLT script) is not the same thing that call xsl_myF4, now we need to change the XPath expression to refer to a set of nodes.
NOTE: Might be an LibXML2 bug, see an explanation here.
RESULTS:

## Using PHP functions with dynamic XSLT

"Real life" templates use XML input data for output. Suppose the following XML:

### XSL sending and receiving string values

To send an input node as string, you can use the XPath-v1.0 string function of the Core Function Library, that converts a node to a string. If the argument is a XML fragment (a node with more than a single value), the "cast to string" replaces tags by blank spaces.
XSL_transf RESULT:

### XSL-registeredFunction communicating by DOM

The most complete way for an XSLT script to send a node as PHP-function parameter, is by sending it without string casting. PHP function will receive as parameter an array of DOMElements, and PHP can send back a DOMElement to the XSLT script.
This is the identity function implemented with this "DOM communication":
Using this function in a loop over input nodes,
XSL_transf RESULT:
Using for lists: a function like xsl_myF5_id can return NULL, producing no interferences. This can be useful for array (or database) composing, later retrieved to XSLT by another function.

## XSLT global parameters

There are more than one way to transfer PHP variables into XSLT, as global parameters:
* calling a php:function that returns the PHP value of the variable;
* Using setparameter in the parser, to create real XSLT-variables from xsl:parameter declaration.
* injecting a "parameter-XML" in the XML input.
The first is perhaps the better, but each has its pros and cons.

### Parameter-specific user-functions

Comparing with setParamter (section below), a function have he advantage of carry XML-fragments (not only string values), but not is accessed by XSLT as an usual variable. Typical use at XSLT:
With something like at PHP:
To return DOM fragments, see section of "XSL receiving external fragments".

### Setting XSLT global parameters

Global parameters are defined on the stylesheet level:
They can have a default value, specified by the select statement. Global parameters can be used to pass values from external applications to the stylesheet.
    TIP: you can use a XPath at select attribute, <xsl:param name="p1" select="."/>, so, take care to use "'string'" when it is not an XPath.
To use the XSLTProcessor::setParameter, rewrite XSL_transf, at the Preparing section:

### XML injection as parameter

Another natural way to read external parameters, is as part of the XML input string. Some DTD-conventions must reviewed, some "array to XML" conventions adopted, and DOM once-insert or replace must processed by the main function (ex. the XSL_transf function above).
It is recommended when there are a lot of parameters or XML fragments. An exemple of use this strategy was the 2.0.2 version of smallest-php-xml-xsl-framework, and "state injection" made there.

## Working with real-life applications

... STANDARD LIB PROPOSAL ...
... See XSLT/Standard-register Functions ...

## Versions and contexts where the examples runs

Please colabore with your tests:
* PHP 5.3.10-1ubuntu3.6 (Zend Engine v2.3.0). All examples runs.

## External links

* LibXML2 details of the "registerModuleFunctions" implementation.
* LibXML2/XSLT page