1

I have the following lines of XSLT. When i try to transform my xml doc I receive the following:

Cannot find a 1-argument function named Q{urn:js}RoundOff()

I believe that there is no issue with the actual script just something that I need to change in the parser setting but I am not sure what to or where to change these settings.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:js="urn:js"
 exclude-result-prefixes="msxsl js">
 
 <msxsl:script language="JavaScript" implements-prefix="js">
  <![CDATA[
        function roundOff(hours) {
            return Math.round(hours * 100) / 100;
        }

        function getPeriodEndDate() {
            var today = new Date();
            var dayOfWeek = today.getDay();

            var tdate = new Date(today.getFullYear(), today.getMonth(), today.getDate());

            if (dayOfWeek === 0) {
                tdate.setDate(tdate.getDate() - 1);
            } else if (dayOfWeek === 1) {
                tdate.setDate(tdate.getDate() - 2);
            } else if (dayOfWeek === 2) {
                tdate.setDate(tdate.getDate() - 3);
            } else if (dayOfWeek === 3) {
                tdate.setDate(tdate.getDate() - 4);
            } else if (dayOfWeek === 4) {
                tdate.setDate(tdate.getDate() - 5);
            } else if (dayOfWeek === 5) {
                tdate.setDate(tdate.getDate() - 6);
            }

            var year = tdate.getFullYear();
            var month = tdate.getMonth() + 1;
            var day = tdate.getDate();

 

            if (month < 10) {
                month = '0' + month;
            }

            if (day < 10) {
                day = '0' + day;
            }

            return year + month + day;
        }
        ]]>
 </msxsl:script>```

....
<Additional lines of xsl>
....


<xsl:value-of select="js:RoundOff(SickHoursAccrued + SickHoursUsed)"  />



Assistance in editing or changing settings to allow the xslt to parse without errors 

2 Answers2

1

Basically, you seem to expect to be able to implement extension functions for XSLT with J(ava)Script, that is not something that Saxon supports.

XSLT 2 and 3, and Saxon 11 is an XSLT 3 processor, have a rich XPath function library shared with XPath and XQuery, and furthermore allow you to declare and implement your own functions in pure XSLT/XPath.

So first check whether any of the functionality you need is available in https://www.w3.org/TR/xpath-functions-31/ (e.g. https://www.w3.org/TR/xpath-functions-31/#func-round), if not, implement it in XSLT/XPath using xsl:function: https://www.w3.org/TR/xslt-30/#stylesheet-functions

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you for your insight. I have a small follow up question based on your comment. The script above is implemented in a program that I am trying to maintain. The issue is I need to make edits but a single edit in that program takes nearly 1-2 hours of processing time and renders editing or attempting to edit incredibly difficult. Is there a way to embed js or c# into xslt using oxygen or another xml processor? – critter1854 Jul 03 '23 at 16:22
  • The Microsoft XSLT processors (both the COM/C++ based MSXML as well as the .NET XslCompiledTransform allow Javascript extension functions, the .NET one also allows extension functions in C# or other .NET languages, although I think in the (new) .NET Core or .NET 6 you can't longer embed C# inline, you need to have your code in a separate assembly and use the processor's API to declare the extension objects. – Martin Honnen Jul 03 '23 at 17:56
0

How about if you edit the transformation scenario you use in Oxygen and in the Transformer combo box you select one of the available MSXML processors instead of Saxon 11? https://www.oxygenxml.com/doc/ug-editor/topics/supported-XSLT-processors-x-2.html

Radu Coravu
  • 1,754
  • 1
  • 9
  • 8