xslt multiple parameters into javascript

0

Im trying to create a function in javascript in xsl which accepts multiple in-parameters. I can not get it to work and get the following error:

Code:   0x80020009
Microsoft JScript runtime error
Wrong number of arguments or invalid property assignment
line = 37, col = 2 (line is offset from the start of the script block).
Error returned from property or method call.

The function looks as follows:

<msxsl:script language="JavaScript" implements-prefix="jsfuncs">
    <![CDATA[
    function getLineLength (x1,x2,y1,y2)
    {
    var xVector = x2 - x1;
    var yVector = y2 - y1;
    var output = Math.sqrt(raised2(xVector)+raised2(yVector));
    return output;
    }
    ]]>
    </msxsl:script>

The calling code looks as follows:

<xsl:value-of select="jsfuncs:getLineLength($x1,$x2,$y1,$y2)"/>

x1,x2... are variables set earlier and they are correct. I can get everything to work when I post-process the values to one parameter. Is it at all possible to pass multiple parameters in xslt to Javascript? The engine in use is msxml 3.0.

xslt
xslt-1.0
asked on Stack Overflow Aug 10, 2016 by Clement Durand

1 Answer

0

Here is an example passing in four parameters:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ms="urn:schemas-microsoft-com:xslt"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="ms mf"
    version="1.0">

    <ms:script language="JScript" implements-prefix="mf">
        function test(a, b, c, d) {
          return a +  ":" + b + ":" + c + ":" + d;
        }
    </ms:script>

    <xsl:template match="/">
        <xsl:value-of select="mf:test(1, 2, 3, 'foo')"/>
        <br/>
        <xsl:variable name="item" select="root/item"/>
        <xsl:value-of select="mf:test(string($item/@a), string($item/@b), string($item/@c), string($item/@d))"/>
    </xsl:template>

</xsl:stylesheet>

As you can see, when wanting to pass in a node to a function expecting a primitive value I would recommend to make sure to convert it on the XSLT side to a primitive value first, as done in mf:test(string($item/@a), string($item/@b), string($item/@c), string($item/@d)), as otherwise the function will get a node-set represented by some selection interface that you would need to iterate first.

The sample works fine for me with MSXML 3 and outputs 1:2:3:foo<br />1:2:3:whatever when run against an input sample with

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <item a="1" b="2" c="3" d="whatever"/>
</root>
answered on Stack Overflow Aug 10, 2016 by Martin Honnen

User contributions licensed under CC BY-SA 3.0