We have an ongoing project where we are porting over an older C# system that used custom functions to augment the XSLT processing, and we were planning on writing it in Node.js/saxon-js.
It seems from the documentation that event though the higher order functions feature is available to JavaScript SaxonJS, that might not be the case for the Node.js subset of it.
Is there an alternative to that, still in the realm of Node.js / server-side JavaScript runtime?
First of all, in XSLT 3 with XPath 3.1 there is a rich built-in function library https://www.w3.org/TR/xpath-functions-31/, furthermore
xsl:functionsince XSLT 2 allows you to implement more functions in XSLT/XPath itself (https://www.w3.org/TR/xslt-30/#stylesheet-functions).As for SaxonJS (tested with the current 2.5 release) and Node.js to call into JavaScript, the following is an example that works for me:
Output e.g.
Documentation https://www.saxonica.com/saxon-js/documentation2/index.html#!development/global and https://www.saxonica.com/saxon-js/documentation2/index.html#!xdm/conversions, although there the
globalThisuse is not mentioned; I believe that is necessary for Node as there, contrary to client-side JavaScript, a globalfunctionorvarstatement does not create a global function or variable.As for processing nodes, I would think it should be save to access the value with e.g.
data()before passing to JavaScript, as inI can't tell for sure how the DOM library used by SaxonJS under Node.js and with the (clojure?) compilation exposes the usual browser know/W3C/WHATWG defined DOM properties and methods; you might need to wait until someone from Saxonica answers that separately or you can consider to ask follow-up questions with more specific details.
As simple
textContentproperty of an element node seems to work here:One final note: for readability and easy understanding above I have always used the XSLT as a JavaScript string and run XSLT by calling the XPath 3.1 function
fn:transformwithSaxonJS.XPath.evaluate; once you have developed your XSLT code for SaxonJS, however, it is highly recommended to "precompile" to SEF, using e.g.xslt3 -t -nogo -xsl:xslt1.xsl -export:xslt1.sef.json -relocate:onand to then useSaxonJS.transform, passing in the the createdsef.jsonfile. That way you get much better performance.