Using Saxon-JS's transform() with sourceNode parameter in node.js

Added by Kai Weber 5 months ago

I am aware that Saxon.JS can behave differently in a browser and a node.js environment: The documentation gives some clear enough examples.

Yet I didn't find a clear statement, that SaxonJS.transform() doesn't work with the sourceNode parameter in node.js, so I assume that it might work, even though I didn't get it to work. I've tested different strategies and now I am wondering if I've overlooked something in the docs, if I am just using the wrong DOM Document implementations, or if it just doesn't work that way.

I've set up a minimal demo project that I attach here: some notes on how to run it can be found in the included file.

What this does is: It runs SaxonJS.transform() with a specific XSLT stylesheet and a specific input file, once using the sourceFileName parameter for the input file, and twice using the sourceNode and sourceBaseURI parameters. With sourceFileName I get the expected output, with the two sourceNode calls I get an empty output with just an XML declaration.

The / template doesn't get matched here, so my first assumption is what the docs say on the sourceNode property:

The principal input as a DOM Node. This will normally be a Document or DocumentFragment node, but any node is acceptable. (If it is not a Document or DocumentFragment, then it will not match any match="/" template rule.)

Yet, when I examine the objects that I pass to this parameter, they quite look like DOM Document nodes to me - and the docs of the DOM implementations I used (xmldom and jsdom) would say they implement the DOM Document class.

Why am I after all this in the first place? Well, I am working in an environment where I have to parse the XML source files anyway. So if I were able to pass the parsed DOM objects to Saxon.transform() I could prevent that the input files need to be parsed twice.

Could anybody tell me if it's worth to keep looking for the right DOM implementation in a node.js environment, or does SaxonJS.transform() in principle not work with DOM Document objects as input in that environment?

Best regards, Kai Weber (39.6 KB) A minimal test case for different source params to SaxonJS.transform()

Replies (2)

RE: Using Saxon-JS's transform() with sourceNode parameter in node.js - Added by Michael Kay 5 months ago

I think that SaxonJS is rather unlikely to work with a DOM implementation other than its own internal one. We can't absolutely say it won't work, but we can absolutely say that we haven't tested it.

I would recommend parsing the XML to a DOM Node using the SaxonJS.getResource() API, to ensure that it uses Saxon's DOM implementation.

RE: Using Saxon-JS's transform() with sourceNode parameter in node.js - Added by Kai Weber 5 months ago

Oh sure, I suspected there's something very obvious that I've overlooked. Certainly my test case works when I load the resource via SaxonJS.getResource(). Thank you!


    Please register to reply