views:

35

answers:

2

Like the title tells: Is it possible to write a PHP-Function in a XSL-Document and call it afterwards?

I have no case, where I wanna do that. Its just a thing it came into my mind while learning XSL.

In XSL you can compose something like:

<xsl:processing-instruction name="php">
   ...some php...
</xsl>

The PHP code will be run in your rendered page. Is it possible to create e.g. a PHP Function in the processing-instruction and call it later (in the same template)?

Pseudo-Sample:

<xsl:template>

   <xsl:processing-instruction name="php">
      ...some php processing $foo...
   </xsl>

   <xsl:variable name="foo" select="xpath/node">

   <xsl:value-of select="call-php-function-with-$foo"/>

</xsl>

I'm looking forward to your solutions/approaches :)

Chris

+1  A: 

The built-in XSL/XSLT module does not have support for functions in PHP either via callback or via code. This does not mean that it cannot be done, just that you are going to have to roll up your sleeves and expose yourself to the internals of both libxslt and of the PHP interpreter if you want to implement it.

Ignacio Vazquez-Abrams
The cause of my thought is, that its clear that you can use common php functions in your xsl-markup, e.g. in a select. So why can't you define your own functions? In my optinion it's not beside the point.
ChrisBenyamin
Those are not PHP functions, those are XSLT/EXSLT functions. They just happen to have the same name.
Ignacio Vazquez-Abrams
A: 

In XSLT 1.0 it is not possible to call functions written in another language, unless these are written following the requirements of the particular XSLT processor for extension functions and specified in the set of available extension functions at the time the transformation is to be initiated.

Functions can be simulated by calling or applying templates.

In XSLT 2.0 it is possible to write functions in XSLT using the <xsl:function> instruction. These functions can then be referenced in any XPath expression that is specified in the select attribute of any other XSLT instructions.

Both in XSLT 1.0 and XSLT 2.0 it is even possible to implement higher-order functions (HOF). This is what the FXSL library (written entirely in XSLT) does.

Dimitre Novatchev