Search code examples
javascriptfunctionvariablesxsltmsxsl

passing xsl variable to javascript function


Using a date:difference funtion from exslt.org date.msxsl.xsl with msxsl. If I use string literals, then the function works fine. But if I grab the strings from the xml file and put them into variables, and then use those variables for the 'difference' function, I get the following error:

msxsl.exe dummy.xml dateDifftest.xsl -o diffOut.html
Error occurred while executing stylesheet 'dateDifftest.xsl'.
Code:   0x80020009
Microsoft JScript runtime error
Wrong number of arguments or invalid property assignment
line = 954, col = 3 (line is offset from the start of the script block).
Error returned from property or method call.

I made 3 tests in this xsl. All 3 use the same datetime strings.

First test calls function with string literals. Second test calls function with variables with the strings as their content. Third test - and failing test - pulls the strings from the xml file.

So how is the variables of the 3rd test different from those of the 2nd test?

If I comment out the function call of the 3rd test this is output:

TEST-1
PT2M1S
TEST-2
2011-12-13T16:15:26
2011-12-13T16:17:27
PT2M1S
TEST-3
2011-12-13T16:15:26
2011-12-13T16:17:27

The xsl file:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
>
<xsl:include href="date.msxsl.xsl" />

<!-- msxsl.exe dummy.xml dateDifftest.xsl -o diffOut.html -->

<xsl:template match="/A/B/C">
    <TEST1>
        TEST-1
    <br/>
        <xsl:value-of select="date:difference('2011-12-13T16:15:26', '2011-12-13T16:17:27')"/>  <!-- outputs "PT2M1S", the difference is 2 mins and 1 sec -->
    </TEST1>
    <br/>
    <TEST2> 
        TEST-2
    <br/>
        <xsl:variable name="startTime" select="'2011-12-13T16:15:26'"/>
        <xsl:value-of select="$startTime"></xsl:value-of>
    <br/>
        <xsl:variable name="endTime" select="'2011-12-13T16:17:27'"/>
        <xsl:value-of select="$endTime"></xsl:value-of> 
    <br/>
        <xsl:value-of select="date:difference($startTime, $endTime)"/>  <!-- also outputs "PT2M1S" -->
    </TEST2>
    <br/>
    <TEST3>
        TEST-3
    <br/>
        <xsl:variable name="startTime" select="start"/>
        <xsl:value-of select="$startTime"></xsl:value-of>
    <br/>
        <xsl:variable name="endTime" select="end"/>
        <xsl:value-of select="$endTime"></xsl:value-of> 
    <br/>
    <!--    <xsl:value-of select="date:difference($startTime, $endTime)"/>--> <!-- FAILS HERE -->


    </TEST3>
</xsl:template>

Input xml file:

<A>
<B>
    <C>
        <start>2011-12-13T16:15:26</start>
        <end>2011-12-13T16:17:27</end>
    </C>
</B>
</A>

Thanks


Solution

  • Looking at the source you have linked to I see

    <doc:args>
        <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg>
        <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg>
    </doc:args>
    

    so I would suggest to try to make sure on the XSLT/XPath side to pass string values as arguments

    <xsl:variable name="startTime" select="string(start)"/>
        <xsl:value-of select="$startTime"></xsl:value-of>
    <br/>
        <xsl:variable name="endTime" select="string(end)"/>
        <xsl:value-of select="$endTime"></xsl:value-of> 
    <br/>
    <xsl:value-of select="date:difference($startTime, $endTime)"/>