SaxonJS on Node.js
Asked Answered
A

2

1

I'm trying to run an empty simple code snippet to test SaxonJS 1.1.0 on NodeJs v8.11.1 on Windows 10.

require('./Saxon-JS-1.1.0/SaxonJS.js');

But I got this error :

PS C:\XXX\sandbox\xsl-transformation> node main.js
C:\XXX\xsl-transformation\Saxon-JS-1.1.0\SaxonJS.js:17136
        setPlatform(JSTestDriver.platform);
                    ^
ReferenceError: JSTestDriver is not defined
at initialize (C:\XXX\sandbox\xsl-transformation\Saxon-JS-1.1.0\SaxonJS.js:17136:25)

Looking at the source code, I can see :

function initialize() {
    "use strict";
    if (inBrowser) {
        setPlatform(BrowserPlatform.platform);
        saxonPrint("Saxon-JS " + getProcessorInfo().productVersion + " in browser", 0);
    } else {
        // Currently only Nashorn. (Later need to distinguish from Node case)
        // Nashorn JSTestDriver
        setPlatform(JSTestDriver.platform);
        saxonPrint("Saxon-JS " + getProcessorInfo().productVersion + " in 
   Nashorn");

        // node NodePlatform
    }

    if (typeof platform.initialize === "function") {
        platform.initialize();
    }
}

It seems Node Platform is not implemented.

However, in the documentation, it is written :

We're talking here primarily about running Saxon-JS in the browser. However, it's also capable of running in server-side JavaScript environments such as Node.js (not yet fully supported in this release).

I deeply search a code snippet of SaxonJS/NodeJS but I did not find one. Has anyone a snippet code of SaxonJS working on NodeJS ?

Araucanian answered 22/6, 2018 at 8:49 Comment(0)
G
1

I'm afraid the documentation was somewhat jumping the gun. We do have users who have reported getting the code to run under Node.js, and we have done it ourselves "in the lab", but it requires source code tweaks to the issued product. As released, the code runs under two platforms, the browser platform and Nashorn (and under Nashorn, it assumes our test harness which is not released).

We're working on a version for Node.js. Doing this properly as a product needs a lot of functionality that isn't in the browser version, for example in XML parsing and serialization, debugging support, command line interfaces and APIs, etc.

Geneva answered 27/6, 2018 at 11:6 Comment(11)
Thanks for you answer. Good luck for the development. :)Araucanian
Hello Michael, I know this is probably an anoying question, but do You have a idea about release dates. 2019, first half, end, 2020?Harewood
It's still some months away. Can't say more than that. We don't work to dates in Saxonica - the code gets released when we think it smells good.Geneva
@MichaelKay what is the status of Saxon-JS on NodeJS now?Spica
Still some months away. We have made good progress. To be honest, part of the problem is that we have so much test material, so we are aware of how many tests are failing, which makes the "release early" approach much more difficult psychologically.Geneva
@MichaelKay just wondering if Saxon-JS on NodeJS is still some time off, or if a beta release might be imminent? Thanks.Gerald
Sorry, please be patient.Geneva
saxon-js v2 has been released on npmjs.org, for NodeJS amongst others, yesterday. npmjs.com/package/saxon-jsImelda
@MichaelKay I can't seem to import 'SaxonJS' on NodeJS application. require('saxon-js') seems to add SaxonJS on 'window' object but couldn't access it from node.jsTouslesmois
@MiloKang please raise this as a new question, not as a comment on an answer to someone else's question.Geneva
@MichaelKay https://mcmap.net/q/1020086/-saxonjs-in-nodejsTouslesmois
C
0

Node.js Saxon-Js Instructions

This S/O question is the first listed on Google for "node.js saxon-js". So I'm answering this 4 years late because of the visibility.

  1. [Terminal] npm install saxon-js
  2. [IDE][xslt.js]
const saxonJs = require('saxon-js');
const fs = require('fs');

function transformDocument(source, destination, transformation, parameters) {
    var xml = fs.readFileSync(source).toString()
    var stylesheetParams = Object.getOwnPropertyNames(parameters)
    .map(o => `QName('', '${o}') : '${parameters[o]}'`).join(",")
    const html = saxonJs.XPath.evaluate(
        `transform(
            map { 
                'source-node' : parse-xml($xml), 
                'stylesheet-location' : $xslt, 
                'stylesheet-params': map {${stylesheetParams}}, 
                'delivery-format' : 'serialized' 
            }
        )?output`, 
        null, 
        {
            params : {
                'xml' : xml, 
                'xslt' : 'file:' + transformation
            } 
        }
    );
    fs.writeFileSync(destination, html)
}

Parameters

  • source: xml file name
  • destination: output file name
  • transformation: xsl file name
  • parameters: regular json object containing any params for xslt

Characteristics

  • (-) extremely slow
  • (+) doesn't require running something on the command line every time the xslt file changes
  • (+) easy to use function signature based on xslt usage over the last 22 years
  • (+) doesn't crash

Notes

  • I didn't find any "Getting Started" for this. Not from Google, at least. I pieced together a working solution from multiple S/O answers.
  • This took me 3 hours. I hope this saves multiple developers 3 hours each.
  • I tried getting 'source-location' to work with a file: url, but no beans
  • sync I/O of course isn't necessary, it should work fine with async and nested callback functions. However, this isn't why its' slow. The xpath transform() function is quite slow, for some reason.
  • For some reason, this solution is stable and doesn't crash. Using Saxon-C over in Python keeps crashing, but this does not.
Cholecystitis answered 27/10, 2022 at 0:53 Comment(3)
@martin-honnen, if there's a way to rewrite my code here without using xpath transform(), but also without having to use the xslt3 cli tool (i.e., converting to sef), I'll update my answer based on your response. I've been using basically this function signature and with no external tools for 22 years, so I'm trying to continue with that methodology, although times have changed. Later maybe I'll learn all about json's new role in xslt transforms.Cholecystitis
Hi @Cholecystitis are you able to provide more details on what failed for you when you were using Saxon/C and python please?Valene
@ond1, I asked a question specifically about the bombing prior to this answer. It's the most details I have. Contrary to the answer I gave, even with clearing parameters it still bombed. I no longer use xslt (opting for pug which resulted in a 900-line xslt being replaced by a 79-line pug file), so I won't be able to reproduce the error for you. But my original code that reproduces the error is in that other question. No matter what I did, it just kept crashing intermittently.Cholecystitis

© 2022 - 2024 — McMap. All rights reserved.