分享

xml

 shineboy1 2015-10-28

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./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.

asked Jun 12 '14 at 1:35

1 Answer

up vote 2 down vote accepted

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

answered Jun 12 '14 at 22:04
John Jin
894
    
Works perfectly. I haven't seen that suggestion anywhere. Thank you. – Russell_Rollins Jun 12 '14 at 22:44
1  
My pleasure. Also please be aware there is another scenario to pass string to js function: '{@startDateTime}', which is not for your case. – John Jin Jun 12 '14 at 22:56
    
Right. You cannot use AVT's inside of select attributes. That is a good general point though. – Russell_Rollins Jun 13 '14 at 15:55

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多