Maestro13
Maestro13

Reputation: 3696

How to specify an inline javascript in xsl to be run in saxon

I have an xslt transformation that works fine when run in Altova XML Spy, but errors in saxon9he.jar. See below for the code snippets and the saxon error message.

What has to be done differently for this to work using saxon as xslt processor?

<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:custom="urn:schemas-microsoft-com:xslt"
    xmlns:js="urn:custom-javascript"
    exclude-result-prefixes="custom js">

    <custom:script language="Javascript" implements-prefix="js">
        <![CDATA[
        function mergeStrings (string1, string2) {
            ... java code ...
        }
    ]]>
    </custom:script>

The custom script is called in xpath expressions like the following:

<xsl:value-of select="js:mergeStrings(str1, str2)" disable-output-escaping="yes"/>

The saxon call I use is like the following:

java -jar C:\saxon9he.jar -s:C:\src.xml -xsl:C:\transform.xslt -o:C:\out.html

Saxon now errors with a message similar to the following:

Error at xsl:value-of on line 775 column 105 of transform.xslt:
  XPST0017: XPath syntax error at char 43 on line 775 in {...gs(str1, str2...}:
    Cannot find a matching 2-argument function named {urn:custom-javascript}mergeStrings()

Upvotes: 0

Views: 2260

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167571

As far as I am aware Saxon does not support writing extension functions in Javascript. You can use the XSLT 2.0 xsl:function to write functions with pure XSLT and XPath 2.0 and you can write extension functions (http://www.saxonica.com/documentation/extensibility/intro.xml) in Java for the Java version of Saxon and with .NET for the .NET version of Saxon. But Javascript is not supported I think.

Upvotes: 2

Related Questions