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

Subject: Re: [xsl] Using saxon-js to run a javasscript function from XSLT at the command line
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 20 May 2025 06:55:10 -0000
> 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

Use the ixsl:call() function documented at

https://www.saxonica.com/saxonjs/documentation3/index.html#!ixsl-extension/fu
nctions/call

You can also use ixsl:window() to get access to the global Javascript object.

A better starting point might be

https://www.saxonica.com/saxonjs/documentation3/index.html#!development/globa
l

I don't know where you got the idea of saxon:script from. You don't include JS
functions within your stylesheet, you include them within the calling JS
application. The simplest thing is to pass in a Javascript object to the
stylesheet as a parameter, then the XPath code can call its methods.

Michael Kay
Saxonica
>
> 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