Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support extension functions written in JavaScript #2

Open
gagern opened this issue Dec 2, 2014 · 10 comments
Open

Support extension functions written in JavaScript #2

gagern opened this issue Dec 2, 2014 · 10 comments

Comments

@gagern
Copy link
Collaborator

gagern commented Dec 2, 2014

It would be nice if we could extend the XSLT resp. XPath language using Extensions functions. LibXSLT provides custom extensions. It would be great to have that API wrapped on the JavaScript side, so one can write extension functions there.

@albanm
Copy link
Owner

albanm commented Dec 2, 2014

Nice ! I will have a look, but I don't have much time for this so I can't make any promise.

Of course a pull request is welcome.

@albanm
Copy link
Owner

albanm commented Dec 16, 2014

Sorry, but things are not looking very good on this topic.

I have done a few tests saved in this commit: 29f1ad6

But I am not convinced that it will bring a satisfactory solution.

@neu-rah
Copy link
Contributor

neu-rah commented Jan 13, 2015

please! i think this is the last obstacle on my port...
please add EXSLT

@gagern
Copy link
Collaborator Author

gagern commented Jan 13, 2015

please add EXSLT

EXSLT is a fixed set of extension functions, and I believe that libxslt supports most of these already. What I'm talking about here is the option to add arbitrary extension functions written in JS.

I have done a few tests saved in this commit: 29f1ad6

Funny, you started at a completely different point than where I'd have started. The fact that there are a large number of drive-by fixes in that commit makes reading it difficult, though.

My first steps were trying to get type conversions implemented. Which is easy enough for simple types, but gets really complicated if you are dealing with nodes. For that one would need to wrap large parts of the libXML API for use from node. As I recall, node-libxslt ships its own version of libXML so it would have to do that as well to provide consistency. Perhaps it would be better to build on some existing libXML wrapper for Node instead? Linking against that might be tricky, but that approach would ensure consistent versions.

But I guess that even if we don't get all types supported yet, this would still be useful for several applications. Having some part of the functionality is better than none at all, imo.

  • How to use functors as extension function for libxslt ?

You don't. The return type of any extension function has to be converted to an XPath data type. So anything which is not boolean, numeric, node or node list (no guarantee that this list is exhaustive) should probably be turned into a string via a simple toString call.

I know XSLT in Java allows passing arbitrary Java objects between extension functions. It might be possible to do the same here as well. But if at all I'd consider this for a later improvement, not for the initial implementation of extension functions.

  • How to refer to a v8 function from async queue worker?
  • Impossible to use an async function as extension function. So no db access, etc.

Async processing is something I hadn't considered at all. I guess the only clean way would be to run the XSLT processor in a separate thread, and to block that thread while the main node thread processes some callback. Once that yields a result, the result could be returned to XSLT. Something like this JS-like pseudocode, but actually written in C:

function callbackWrapper(args) {
  create mutex m and lock it.
  var res;
  function doCallback() {
    res = callback(args);
    unlock m;
  }
  enqueue doCallback on main event queue.
  wait for m to become unlocked.
  return res.
}

If the callback throws an exception, that might make things even more difficult. In an ideal world, any exception raised by the callback would cause the XSLT process to abort and would get passed to the error event handler of the XSLT event dispatcher. I'm not sure whether libxslt is properly reentrant. If not, then we'd have to make sure that we don't start more than one XSLT process at the same time. But that's a general issue, not specific to extension functions, right?

@albanm
Copy link
Owner

albanm commented Jan 13, 2015

@neu-rah EXSLT is now supported in version 0.3.0. Actually all required libraries were already included, extension functions were simply not registered. Anyway this is fixed and published.

@gagern I need a little more time to refresh my memories and write down an answer. But I will come back !

@neu-rah
Copy link
Contributor

neu-rah commented Jan 14, 2015

ok, built in module functions are working (at least I've tested with evaluate, Ok).
looking forward for user functions
great work, thanks!

@gagern
Copy link
Collaborator Author

gagern commented Jan 30, 2015

Here is my current work in progress. It's already usable to some degree: I can pass values of type boolean, number and string in both directions, and I can call the custom function in synchroneous and in asynchroneous context. Supporting other types, nodes and node sets in particular, will probably require more work since these need to be properly wrapped. But as a starting point I'm quite happy with this here.

@tasoss
Copy link

tasoss commented May 23, 2016

Hello.I'm trying to use regular expressions in my xslt.Is there any way to achieve this?Thanks!

@gagern
Copy link
Collaborator Author

gagern commented May 23, 2016

@tasoss: node-libxslt supports exslt, which supports regular expressions. Should not be a problem, but it's qzite distinct from what this issue here is about.

@tasoss
Copy link

tasoss commented May 23, 2016

Thanks for answering...Sorry for posting here.I will try to post on a forum/mailing list or something because i need more help on reg. expressions.Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants