Define a PHP function in XSL and call it. Possible? How?

As in the title: is it possible to write a PHP function in an XSL document and call it later?

I have no business where I want to do this. This is just what occurred to me when learning XSL.

In XSL, you can compose something like:

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

PHP code will run on your displayed page. Is it possible to create, for example, 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 look forward to your decisions / approaches :)

Chris

+4
source share
3 answers

In XSLT 1.0, it is not possible to call functions written in another language if they are not written in accordance with the requirements of a particular XSLT processor for extension functions and are not specified in the set of available extension functions at the time at which the conversion should be initiated.

Functions can be modeled by calling or applying templates.

In XSLT 2.0, you can write functions in XSLT using <xsl:function> . These functions can then be referenced in any XPath expression specified in the select attribute of any other XSLT statements.

In both 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.

-2
source

I think the selected answer is pointing in the wrong direction. No need to use FXSL, you can easily add extensions written in other languages ​​to XSLT - for example, Jython or Javascript with Xalan, Java with most Java processors, etc.

As for PHP, if you are doing XSLT processing in a PHP script (unlike, say, Xalan running from Ant or something else), then it’s very simple to use XSLTProcessor :: registerPHPFunctions, which allows you to write things like

 $xsl = <<<EOF ... <xsl:value-of select="php:function('myFunc', . )"/> ... EOF; function myFunc( $node ) { //etc 

Of course, only a PHP script will be able to handle XSL correctly.

All of this is pretty clearly documented at http://www.php.net/manual/en/xsltprocessor.registerphpfunctions.php

+4
source

This can be done (remember Dimitre that if he asks about PHP, he will almost certainly be limited to XSLT 1.0, and FXSL is not quite what he is after).

Firstly, if you plan to write PHP yourself and use it as part of the document processing flow, this is probably a poor choice of language for this. In the case where you just want to extend the tiny bit XSL functionality with a few PHP functions from your own or useful library functions, such as preg_replace, so you can call them from your XSLT, you are absolutely fine if you know that these are in advance: you export PHP functions to XSLT and just call them from there.

If the code you want to execute is different for each document, however, it becomes more complicated (i.e. to process the instructions in the documents you are trying to process that contain PHP, and you want the XSLT script to evaluate your document for follow the processing instructions). Try and drop the functions in the well-known set and just call them from PHP, but if you cannot and want to try, it is up to the hacker.

(I assume that you know about the way to export functions to XSLT in PHP: you execute some regular PHP functions, you export them to an XSLT script using registerPHPFunctions, and they just call them functions inside XSLT. Beaten path, check the docs.)

Let's get back to executing the PHP code in your PI documents from an XSLT script.

(I didn’t do it myself, but it should not be so difficult to implement.) I believe that this cannot be done in one pass. You must call the XSLT processor, and whenever you click on the PHP you want to run, enter a marker tag (for example, <phpmarker md5ofcode="php:md5(...)"> ) and pass the code back to the calling PHP. Hacky, but potentially livable, would be to put it in an xsl: message and catch it from a custom handler in PHP.

When the first run is complete, do a little string manipulation to make a wrapper function around each block of code that you grab, evaluate that line to create functions, export them to the XSLT engine, and then call it again with a second XSLT script that will quote marker nodes and call PHP on their contents.

Nasty? Yes. You might want to explain why you want this, but it should be possible (in two passes) to make it work.

Edit : After rereading my question a few more times, I am increasingly embarrassed. Are you sure you want to use PI in your XSLT? PI go in documents explaining how to handle them. If PHP is intended to be used in an XSLT file and that is not a typo, I think you might be a little confused, and it is just a simple application for PHP vanillin registerPHPFunctions. You just need to put PHP in a separate file and call it from XSLT, which is not a huge burden, since XSLT scripts do not have to contain many functions before they are large enough to split into two files.

+1
source

Source: https://habr.com/ru/post/1316022/


All Articles