Calling jQuery-UI components in XSL using Saxon-JS and its global Javascript function namespace?

六眼飞鱼酱① 提交于 2019-12-07 00:31:34

It seems your xsl:value-of Javascript call is evaluated before the result element is created or at least inserted in the document.

What has worked for me is to simply put a script result element after the input, creating the Javascript code on the fly:

<xsl:template match="date">
    <tr>
        <td>Date:</td>
        <td>
            <xsl:variable name="currentValue" select="string-join((@month, @day, @year), '/')"/>
            <xsl:value-of select="$currentValue"/>
            <br/>
            <input type="text" id="datepicker{position()}"/>

            <script xsl:expand-text="yes">initDP('#datepicker{position()}', '{$currentValue}')</script>

        </td>
    </tr>
</xsl:template>

Tested successfully with Firefox from the file system and over HTTP and with Chrome over HTTP (Chrome by default doesn't do XMLHttpRequest over the file system so Saxon-JS over the file system does not work). Edge does not seem to execute the XSLT and gives two "SCRIPT5022: XError: Misplaced or malformed XML" errors in SaxonJS.min.js (1,11772), IE seems to execute the stylesheet but the inline script does not seem to be executed. Online sample at https://martin-honnen.github.io/xslt/2017/ui-test1.html.

I have now also successfully test a different approach (https://martin-honnen.github.io/xslt/2017/ui-test2.xsl) with Firefox, Chrome and Internet Explorer that instead of an inline script result element uses ixsl:schedule-action to call a template that then uses js:initDP:

<xsl:template match="date">
    <tr>
        <td>Date:</td>
        <td>
            <xsl:variable name="currentValue" select="string-join((@month, @day, @year), '/')"/>
            <xsl:value-of select="$currentValue"/>
            <br/>
            <input type="text" id="datepicker{position()}"/>
            <ixsl:schedule-action wait="1">
                <xsl:call-template name="init-datepicker">
                    <xsl:with-param name="id" select="'#datepicker' || position()"/>
                    <xsl:with-param name="value" select="$currentValue"/>
                </xsl:call-template>
            </ixsl:schedule-action>                
        </td>
    </tr>
</xsl:template>

<xsl:template name="init-datepicker">
    <xsl:param name="id" as="xs:string"/>
    <xsl:param name="value" as="xs:string"/>
    <!--<xsl:sequence select="trace(js:initDP($id, $value), 'init-datepicker called for ' || $id || ' at ' || current-dateTime())"/>-->
    <xsl:sequence select="js:initDP($id, $value)"/>
</xsl:template>

The Edge problem seems to unrelated to the issue here to have Saxon-JS and JQuery UI interact, instead it is appears to be caused by a bug in the Javascript engine of the current Edge version, see https://github.com/Microsoft/ChakraCore/issues/3366, which the Saxon-JS code runs into when checking for a misplaced XML declaration. If I remove the XML declarations in the XML input and Saxon's SEF package file then https://martin-honnen.github.io/xslt/2017/ui-test4.html works fine in Edge as well as that way the Saxon-JS code does not run into the Edge Javascript bug.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!