1

Okay, so I'm having trouple with using javascript scripts to perform calculations on my XSLT transformation.

Basically, there is an xml element in the file to be transformed, with attributes startDateTime and endDateTime. All I want to do is subtract the two, and return that value for the user to see.

    <event startDateTime = "" endDateTime = "">    
      <eventOutcome>Fail</eventOutcome>
    <event>

Where obviously, everything would be filled in correctly. Then, there is an xslt transformation, that needs to do the math to find the elapsed time.

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"> //schemas included here

        <msxsl:script language="JScript" implements-prefix="user">
            function elapsedTime(start, end) {         
                return (xmlToDateTime(end).getTime() - xmlToDateTime(start).getTime()) / 1000;
            }

            function xmlToDateTime(xmlString) {
                //a script that works fine
            }
        </msxsl:script>

        <xsl:output method="xml" indent="yes"/>

        <xsl:template match="topLevelElement">
            <wordDocument>      
                 <body>
                    <sect>
                        <p>
                            <r>
                                <t><xsl:apply-templates select="event"/> Seconds</w:t>
                            </r>            
                        </p>          
                     <sect>
                <body>
            <wordDocument>
        </xsl:template>

        <xsl:template match="event">    
            <xsl:value-of select="user:elapsedTime(@startDateTime, @endDateTime)"/>    
        </xsl:template>

    </xsl:stylesheet>

What I want this to do is pass the attributes of event to the javascript function as strings. What it does instead, is pass an object of that Visual Studio identifies as {MS.Internal.Xml.XPathArrayIterator}. The function works fine if I use the strings directly like

<xsl:value-of select="user:elapsedTime(DATETIME', 'DATETIME')

So the problem isn't the javascript itself, but more my ability to send it arguments.

The official MS Documentation is less than helpful.

http://msdn.microsoft.com/en-us/library/533texsx(v=vs.110).aspx

For one, they use C# instead of JS, which I'm not sure the significance of. But they are just passing the name of an element in, no explanation of how it gets that value out.

I also read the FAQ here

http://www.dpawson.co.uk/xsl/sect4/N9745.html#d13958e70

And their code doesn't work! It gives the same issue of passing a {MS.Internal.Xml.XPathArrayIterator} instead of a value.

I'm at a loss here, so I'm open to any suggestions. Thanks in advance.

1 Answer 1

2

To pass string to a javascript function, you can use string function as : string(@startDateTime), string(@endDateTime)

Sign up to request clarification or add additional context in comments.

3 Comments

Works perfectly. I haven't seen that suggestion anywhere. Thank you.
My pleasure. Also please be aware there is another scenario to pass string to js function: '{@startDateTime}', which is not for your case.
Right. You cannot use AVT's inside of select attributes. That is a good general point though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.