Skip to content

Instantly share code, notes, and snippets.

@EmmanuelOga
Last active December 1, 2020 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmmanuelOga/5f4cc30f73d7b09398349b48382d3514 to your computer and use it in GitHub Desktop.
Save EmmanuelOga/5f4cc30f73d7b09398349b48382d3514 to your computer and use it in GitHub Desktop.
temp
node_modules

Sample Saxon/JS extension function

  • Clone
  • yarn install to get dependencies
  • yarn compile to generate sef file (if you change the .xsl file)
  • node index.js to run the example.

Node v14.10.0, Win10 64

Without changes:

(base) PS C:\dev\random\saxonjs-global-examples\01-global-functions> node .\index.js
Transformation failure: ReferenceError: window is not defined
(node:7172) UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
    at Object.window (C:\dev\random\saxonjs-global-examples\01-global-functions\node_modules\saxon-js\SaxonJS2N.js...)
    ....
    at C:\dev\random\saxonjs-global-examples\01-global-functions\node_modules\saxon-js\SaxonJS2N.js:5463:480
    at async start (C:\dev\random\saxonjs-global-examples\01-global-functions\index.js:23:33)

Now open index.js and uncomment these lines:

global["window"] = {extensionFunction};

New result:

$ node .\index.js
INPUT:  <root></root>
OUTPUT: <?xml version="1.0" encoding="UTF-8"?><result>Hello from JavaScript</result>
const fs = require("fs");
const SaxonJS = require("saxon-js");
function extensionFunction() {
return "Hello from JavaScript";
};
async function start() {
const xml = `<root></root>`;
const xsltPath = "./test.sef.json";
// Uncomment this for saxon to work:
// global["window"] = {extensionFunction};
const saxonOptions = {
stylesheetText: fs.readFileSync(xsltPath),
sourceText: xml,
sourceFileName: "inline.xml",
destination: "serialized",
};
try {
const { principalResult } = await SaxonJS.transform(
saxonOptions,
"async"
).catch((e) => {
throw e;
});
console.log(`INPUT: ${xml}`);
console.log(`OUTPUT: ${principalResult}`);
} catch (e) {
throw e;
}
}
start();
{
"dependencies": {
"saxon-js": "^2.0.2",
"xslt3": "^2.0.2"
},
"scripts": {
"compile": "xslt3 -t -xsl:test.xsl -export:test.sef.json -nogo"
}
}
{"N":"package","version":"30","packageVersion":"1","saxonVersion":"Saxon-JS 2.0","target":"JS","targetVersion":"2","name":"TOP-LEVEL","relocatable":"true","buildDateTime":"2020-12-01T15:18:12.768-08:00","ns":"xml=~ xsl=~ js=~","C":[{"N":"co","binds":"","id":"0","C":[{"N":"mode","onNo":"TC","flags":"","patternSlots":"0","prec":"","C":[{"N":"templateRule","rank":"0","prec":"0","seq":"0","ns":"xml=~ xsl=~ js=~","minImp":"0","flags":"s","baseUri":"file://C:/dev/random/saxonjs-global-examples/01-global-functions/test.xsl","slots":"200","line":"7","module":"test.xsl","match":"root","prio":"0","matches":"NE nQ{}root","C":[{"N":"p.nodeTest","role":"match","test":"NE nQ{}root","sT":"1NE nQ{}root","sType":"1NE nQ{}root","ns":"= xml=~ fn=~ xsl=~ js=~ "},{"N":"elem","name":"result","sType":"1NE nQ{}result ","nsuri":"","namespaces":"","role":"action","line":"8","C":[{"N":"valueOf","flags":"l","sType":"1NT ","line":"9","C":[{"N":"fn","name":"string-join","role":"select","C":[{"N":"forEach","sType":"*AS ","C":[{"N":"data","sType":"*A ","C":[{"N":"mergeAdj","C":[{"N":"ifCall","sType":"*","name":"Q{http://saxonica.com/ns/interactiveXSLT}call","sT":"*","ns":"= xml=~ fn=~ xsl=~ js=~ ex=~ ","role":"select","line":"9","C":[{"N":"ifCall","name":"Q{http://saxonica.com/ns/interactiveXSLT}window","sT":"1"},{"N":"str","val":"extensionFunction"},{"N":"arrayBlock","sT":"1FA"}]}]}]},{"N":"fn","name":"string","sType":"1AS ","C":[{"N":"dot"}]}]},{"N":"str","sType":"1AS ","val":" "}]}]}]}]}]}]},{"N":"overridden"},{"N":"output","C":[{"N":"property","name":"Q{http://saxon.sf.net/}stylesheet-version","value":"31"}]},{"N":"decimalFormat"}],"Σ":"e34fdbd8"}
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="3.1"
exclude-result-prefixes="#all"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:js="http://saxonica.com/ns/globalJS">
<xsl:template match="root">
<result>
<xsl:value-of select="js:extensionFunction()" />
</result>
</xsl:template>
</xsl:stylesheet>
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
dependencies:
follow-redirects "1.5.10"
debug@=3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"
follow-redirects@1.5.10:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"
ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=
saxon-js@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/saxon-js/-/saxon-js-2.0.2.tgz#92579d793ae6ceb301881e49103f6ad4a26a6ce7"
integrity sha512-Pfz4EZHoB8/mMoC2SzULm4ESZp+67OiCxoAwmYDwkEVdaiWtbU+ubmo/UXM/EXM9OsIyQ9Vs8NAAPCZuOHkhtA==
dependencies:
axios "^0.19.2"
xslt3@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/xslt3/-/xslt3-2.0.2.tgz#b12d5d45f71b8ddbed30403eefd5abed035f95f5"
integrity sha512-8rUX1wjiYFiSKFbcBXSiO2AtWB1DsRxv9Uk98TNvnu+Euy0CJnm8xKuCsx6ZtBET7Z/WsK+MFfXpUsQ+kDh0dg==
dependencies:
axios "^0.19.2"
saxon-js "^2.0.2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment