[xsl] Using saxon-js to run a javasscript function from XSLT at the command line

Subject: [xsl] Using saxon-js to run a javasscript function from XSLT at the command line
From: "dvint@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 20 May 2025 05:09:20 -0000
I'm trying to put this together from various sources on the web and none of them seem to be complete. Main piece missing is how to connect the js to the stylesheet. I'm just trying to get a simple exsmple working before tackling my main problem.

This is my command line. I can't get the xslt3 command to work like the examples show, but this gets me there:

node <ptah>/node_modules/xslt3/xslt3.js -xsl:test-js.xsl -s:test-js.xsl

Gives me:

Error XPST0017:
Static error in XPath on line 20 in _betterXML/test-js.xsl {saxon:call('greet', $name)}: Unknown function Q{http://saxon.sf.net/}call()
Failed to compile stylesheet


STYLESHEET test-js.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	xmlns:xs="http://www.w3.org/2001/XMLSchema";
	xmlns:mf="http://example.org/my-functions";
	xmlns:js="http://saxonica.com/ns/globalJS";
	xmlns:saxon="http://saxon.sf.net/";
	xmlns="http://www.w3.org/2005/xpath-functions";
	exclude-result-prefixes="xs js ping mf"
	version="3.0">

<xsl:output method="xml" indent="yes"/>

<saxon:script src="my-functions.js"/>

  <xsl:function name="mf:greet" as="xs:string">
    <xsl:param name="name" as="xs:string"/>
    <xsl:sequence select="saxon:call('greet', $name)"/>
  </xsl:function>

  <xsl:template match="/">
    <result>
      <xsl:value-of select="mf:greet('World')"/>
    </result>
  </xsl:template>
 <xsl:stylesheet>


my-functions.js


 // my-functions.js
function greet(name) {
  return "Hello, " + name + "!";
}

Stylesheet and JS file are in the same folder. I'm using the stylsheet as the data file as the stylesheet isn't really doing any processing. I suspect this isn't correct, but something is needed to connect the stylesheet to the JS file like this

<saxon:script src="my-functions.js"/>

..dan

Current Thread