PHP callback from XSLT

  Status: implemented,  Tue Jan 2 14:56:57 CET 2007
  • Created: 2006-12-28 19:36:40+0100
  • Categories: xml
  • Author: kiesel

Scope of Change

Enable callbacks to PHP functions from within XSL through the php:function() callback supplied by PHP 5.


Rationale

Sometimes complex functionality can be (more) easily calculated with PHP rather than with XSL. Even more, sometimes results depend on huge amount of information that would need to be transferred into XML to be available in the XSL transformation.

Having callbacks eases complex calculations and circumvents the need to have large data serialized (bloated) into XML.

Functionality

The DomXSLProcessor will have a method registerInstance() where a PHP object instance can be registered under a defined name:

  $myContext= new WebsiteContext();
// ...

$processor= new DomXSLProcessor();
$processor->registerInstance('context', $myContext);
// ...

From within XSL one can call the an invoke method which takes define name, method name and method arguments and invokes the given method in a non-static context:

  <?xml version="1.0" encoding="iso-8859-1"?>
  <xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:php="http://php.net/xsl"
  >
    <xsl:template match="/">
      <xsl:value-of select="php:function(
        'XSLCallback::invoke', 
        'context', 
        'hasPermission', 
        'view'
      )"/>
    </xsl:template>
  </xsl:stylesheet>

This invokation translates into the PHP function call on a method hasPermission() with the argument "view" on the object formerly registered as "context". The method must have the annotation @xslmethod:

  class WebsiteContext extends Object {

/**
* XSL callback function
*
* @param string permission
* @return bool
*/
#[@xslmethod]
public function hasPermission($permission) {
return TRUE;
}
}

Security considerations

As every callable method must be explicitely annotated fine grained control over methods that can be called is achieved and thus good security.

Speed impact

When not using the callback, there's no impact. When using it, there's a negative impact through the context switch from XSL to PHP which is believed to be more than equalized by the advantage of faster calculations in PHP and the removal of lots of XML nodes in the tree.

Dependencies

n/a

Related documents

- http://xp-framework.net/rfc/contrib/rfc0104.diff

Comments

- kiesel, Thu Dec 28 19:52:56 CET 2006 We could also just allow every PHP method to be called, simplifying this callback issue. But, then we have no control over what is called.

- friebe, Fri Dec 29 11:18:39 2006 Why is the annotation named "xml"callback while it actually is a callback from the XSL processor - shouldn't it be called "xsl"callback?

- kiesel, Fri Dec 29 11:34:53 CET 2006 I don't like the word callback, so the annotation's name was changed to "xslmethod".

- kiesel, Fri Dec 29 14:26:38 CET 2006 Changed RFC to simplify using existing objects within XSL, does not longer use special facades. Patch was updated.

- kiesel, Fri Dec 29 14:27:49 CET 2006 Now, through callback functions there can be all kinds of exceptions. Should they be caught and re-wrapped in a TransformerException?

<EOF>


Table of contents