Re: Passing a "callback" name to ixsl:schedule-action

Øyvind Gjesdal <[email protected]>
Newsgroups gmane.text.xml.saxon.help
Message-ID <CAOB384ayi8-9+A_TPGus+_pA1sXTuLr-bKC6sTCjoYGjcV6=jQ@mail.gmail.com>
I tried to fork the repo but didn't manage to get any further. I think I
misunderstood the original email/stylesheet and jumped to the wrong
conclusions.

Kind regards,
Øyvind


søn. 30. aug. 2020 kl. 17:28 skrev Martynas Jusevičius <
martynas-tyFqBaoSXPC1Z/[email protected]>:

> On Sun, Aug 30, 2020 at 5:15 PM Øyvind Gjesdal <[email protected]> wrote:
>
>> Then I think the result-document needs to be called from the function
>> from calling or applying a template from the function.
>>
>
> Could you rephrase this? Have read it multiple times but haven't
> understood yet :)
>
>
>> I call a named template from a function here, where the named function
>> writes the result:
>>
>> https://gitlab.com/oyvindg/search-nb/-/blob/master/xsl/lib/saxon-js-utils.xsl#L25
>>
>>
>> søn. 30. aug. 2020 kl. 16:43 skrev Martynas Jusevičius <
>> martynas-tyFqBaoSXPC1Z/[email protected]>:
>>
>>> Øyvind,
>>>
>>> I get "Cannot call xsl:result-document while evaluating function" with
>>> your example.
>>>
>>> On Sun, Aug 30, 2020 at 1:14 PM Øyvind Gjesdal <[email protected]>
>>> wrote:
>>>
>>>> I wonder if it is because the 'xsl:message' is not inside a
>>>> result-document fragment (using an id fragment from the input.html such as
>>>> 'test' on button element ). From the documentation (
>>>> https://www.saxonica.com/saxon-js/documentation/index.html#!ixsl-extension/instructions/schedule-action
>>>> ):
>>>>
>>>> Note that ixsl:schedule-action does not write the value returned by
>>>> the contained xsl:call-template instruction to the current result
>>>> tree. In practice therefore, the only useful thing that the called template
>>>> can do is to issue an xsl:result-document instruction (or similar
>>>> instructions with side-effects, such as ixsl:set-attribute).
>>>>
>>>> Does this work?
>>>> <xsl:function name="ac:test">
>>>> <xsl:param name="response" as="map(*)"/>
>>>> <!-- default mode for result-document is append, only console message
>>>> should be added-->
>>>> <xsl:result-document href="#test">
>>>> <xsl:message>RESPONSE STATUS: <xsl:value-of select="$response?status"
>>>> /></xsl:message>
>>>> <xsl:call-template name="another-template"/>
>>>> </xsl:result-document>
>>>> </xsl:function>
>>>>
>>>> Kind regards,
>>>> Øyvind
>>>>
>>>>
>>>> lør. 29. aug. 2020 kl. 16:26 skrev Martynas Jusevičius <
>>>> martynas-tyFqBaoSXPC1Z/[email protected]>:
>>>>
>>>>> I've created a minimal case which still exhibits the "Internal error:
>>>>> Target component not found (slot=0 id=NaN)" error:
>>>>>
>>>>> https://namedgraph.github.io/saxon-js2-test/
>>>>> Source: https://github.com/namedgraph/saxon-js2-test/tree/gh-pages
>>>>>
>>>>> On Fri, Aug 28, 2020 at 4:38 PM Michael Kay <mike-JkSD5nQpfvpWk0Htik3J/[email protected]> wrote:
>>>>> >
>>>>> > It should be possible. "Target component not found" tends to suggest
>>>>> an internal error.
>>>>> >
>>>>> > Michael Kay
>>>>> > Saxonica
>>>>> >
>>>>> > On 28 Aug 2020, at 14:34, Martynas Jusevičius <
>>>>> martynas-tyFqBaoSXPC1Z/[email protected]> wrote:
>>>>> >
>>>>> > OK, got it.
>>>>> >
>>>>> > Can I use <xsl:call-template> from within such a function? The
>>>>> > following one gives me the "Target component not found" error
>>>>> >
>>>>> >    <xsl:function name="apl:resource-typeahead-callback">
>>>>> >        <xsl:param name="element" as="element()"/>
>>>>> >        <xsl:param name="container-uri" as="xs:anyURI"/>
>>>>> >        <xsl:param name="response" as="map(*)"/>
>>>>> >
>>>>> >        <xsl:choose>
>>>>> >            <xsl:when test="$response?status = 200">
>>>>> >                <xsl:for-each select="$response?body">
>>>>> >                    <xsl:call-template name="typeahead:process">
>>>>> >                        <xsl:with-param name="menu" select="$menu"/>
>>>>> >                        <xsl:with-param name="items"
>>>>> > select="rdf:RDF/*[@rdf:about[not(. =
>>>>> >
>>>>> $container-uri)]][not(core:stateOf)][not(core:viewOf)][not(dh:pageOf)][not(ldt:paramName)]"/>
>>>>> >                        <xsl:with-param name="element"
>>>>> select="$element"/>
>>>>> >                        <xsl:with-param name="name" select="'ou'"/>
>>>>> >                    </xsl:call-template>
>>>>> >                </xsl:for-each>
>>>>> >            </xsl:when>
>>>>> >            <xsl:otherwise>
>>>>> >                <xsl:value-of select="ixsl:call(ixsl:window(),
>>>>> > 'alert', [ $response?message ])"/>
>>>>> >            </xsl:otherwise>
>>>>> >        </xsl:choose>
>>>>> >    </xsl:function>
>>>>> >
>>>>> > No error if I comment out the <xsl:call-template>.
>>>>> >
>>>>> > Do I now need to turn all the named templates in the library into
>>>>> functions?
>>>>> >
>>>>> > On Fri, Aug 28, 2020 at 12:35 PM Michael Kay <mike-JkSD5nQpfvpWk0Htik3J/[email protected]>
>>>>> wrote:
>>>>> >
>>>>> >
>>>>> > <xsl:variable name="callback"
>>>>> select="apl:resource-typeahead-callback#0"/>
>>>>> >
>>>>> > if it has 0 parameters, apl:resource-typeahead-callback#1 if it has
>>>>> one parameter, etc.
>>>>> >
>>>>> > A global function (declared with xsl:function) won't have any data
>>>>> in its closure. If you want to capture state in the function, you need an
>>>>> anonymous function:
>>>>> >
>>>>> > <xsl:variable name="someData" select="doc('abc.xml')//something"/>
>>>>> > <xsl:variable name="callback"
>>>>> select="function(){my:do-something-with($someData)}"/>
>>>>> >
>>>>> > Here the value of $someData is "remembered" by the function object
>>>>> (in its "closure") and can therefore be referenced later, when the function
>>>>> is executed (i.e. in your case, when the HTTP request is completed).
>>>>> >
>>>>> > This doesn't play well with mutable objects, so try to avoid holding
>>>>> references to the HTML page in the closure.
>>>>> >
>>>>> > Michael Kay
>>>>> > Saxonica
>>>>> >
>>>>> > On 28 Aug 2020, at 11:23, Martynas Jusevičius <
>>>>> martynas-tyFqBaoSXPC1Z/[email protected]> wrote:
>>>>> >
>>>>> > Thanks. I'm not yet familiar with higher-order functions in XSLT.
>>>>> >
>>>>> > Lets say I want to use <xsl:function
>>>>> > name="apl:resource-typeahead-callback"> as the callback -- how do I
>>>>> > supply it as $callback?
>>>>> >
>>>>> > On Fri, Aug 28, 2020 at 11:23 AM Michael Kay <mike-JkSD5nQpfvpWk0Htik3J/[email protected]>
>>>>> wrote:
>>>>> >
>>>>> >
>>>>> > This is very much in the whole area of my Balisage 2020 paper an
>>>>> Asynchronous XSLT, and of course that describes the limitations of the
>>>>> current design which this use case illustrates well.
>>>>> >
>>>>> > You're constrained to invoke a statically-known template within
>>>>> ixsl:schedule-action, but the parameters to that template can include a
>>>>> function item which is invoked dynamically. So you can do something like
>>>>> >
>>>>> > <ixsl:schedule-action http-request="....">
>>>>> > <xsl:call-template name="http-completion">
>>>>> >    <xsl:with-param name="action" as="function(*)"
>>>>> select="$callback"/>
>>>>> > </xsl:call-template>
>>>>> > </ixsl:schedule-action>
>>>>> >
>>>>> > <xsl:template name="http-completion">
>>>>> > <xsl:param name="action" as="function(*)"/>
>>>>> > <xsl:sequence select="$action()"/>
>>>>> > </xsl:template>
>>>>> >
>>>>> > Michael Kay
>>>>> > Saxonica
>>>>> >
>>>>> > On 28 Aug 2020, at 10:13, Martynas Jusevičius <
>>>>> martynas-tyFqBaoSXPC1Z/[email protected]> wrote:
>>>>> >
>>>>> > Hi,
>>>>> >
>>>>> > with Saxon-CE I've used a typeahead "library" which would accept both
>>>>> > the JS function to be called and the callback as params:
>>>>> >
>>>>> > <!-- caller -->
>>>>> >
>>>>> > <xsl:call-template name="typeahead:load-xml">
>>>>> >  <xsl:with-param name="element" select="."/>
>>>>> >  <xsl:with-param name="query" select="$text"/>
>>>>> >  <xsl:with-param name="uri" select="$results-uri"/>
>>>>> >  <xsl:with-param name="js-function" select="$js-function"/>
>>>>> >  <xsl:with-param name="callback" select="ixsl:get(ixsl:window(),
>>>>> > 'onresourceTypeaheadCallback')"/>
>>>>> > </xsl:call-template>
>>>>> >
>>>>> > <!-- library -->
>>>>> >
>>>>> > <xsl:template name="typeahead:load-xml">
>>>>> >  <xsl:param name="element" as="element()"/>
>>>>> >  <xsl:param name="uri" as="xs:anyURI"/>
>>>>> >  <xsl:param name="query" as="xs:string"/>
>>>>> >  <xsl:param name="js-function" as="xs:string"/>
>>>>> >  <xsl:param name="callback"/>
>>>>> >  <!-- if the value hasn't changed during the delay -->
>>>>> >  <xsl:if test="$query = $element/ixsl:get(., 'value')">
>>>>> >      <xsl:value-of select="ixsl:call(ixsl:window(), $js-function, [
>>>>> > ixsl:event(), $uri, $callback ])"/>
>>>>> >  </xsl:if>
>>>>> > </xsl:template>
>>>>> >
>>>>> > Now I want to replace all custom HTTP-related functions with
>>>>> > <ixsl:schedule-action http-request="">. So I picture something like
>>>>> > this:
>>>>> >
>>>>> > <xsl:template name="typeahead:load-xml">
>>>>> >  <xsl:param name="element" as="element()"/>
>>>>> >  <xsl:param name="uri" as="xs:anyURI"/>
>>>>> >  <xsl:param name="query" as="xs:string"/>
>>>>> >  <!-- if the value hasn't changed during the delay -->
>>>>> >  <xsl:if test="$query = $element/ixsl:get(., 'value')">
>>>>> >      <xsl:schedule-action http-request="map{ 'method': 'GET',
>>>>> > 'href': $uri, 'headers': map{ 'Accept': 'application/rdf+xml' } }">
>>>>> >          <xsl:call-template name="?????????"/>
>>>>> >      </ixsl:schedule action>
>>>>> >  </xsl:if>
>>>>> > </xsl:template>
>>>>> >
>>>>> > So <xsl:call-template> functions like a callback here, but I cannot
>>>>> > pass template name as a param. Of course I could hardcode some name
>>>>> > like "typeahead:xml-loaded", but I would prefer not doing that.
>>>>> >
>>>>> > Is there an idiomatic Saxon-JS solution to this?
>>>>> >
>>>>> >
>>>>> > _______________________________________________
>>>>> > saxon-help mailing list archived at http://saxon.markmail.org/
>>>>> > [email protected]
>>>>> > https://lists.sourceforge.net/lists/listinfo/saxon-help
>>>>> >
>>>>> >
>>>>> > _______________________________________________
>>>>> > saxon-help mailing list archived at http://saxon.markmail.org/
>>>>> > [email protected]
>>>>> > https://lists.sourceforge.net/lists/listinfo/saxon-help
>>>>> >
>>>>> >
>>>>> >
>>>>> > _______________________________________________
>>>>> > saxon-help mailing list archived at http://saxon.markmail.org/
>>>>> > [email protected]
>>>>> > https://lists.sourceforge.net/lists/listinfo/saxon-help
>>>>> >
>>>>> >
>>>>> > _______________________________________________
>>>>> > saxon-help mailing list archived at http://saxon.markmail.org/
>>>>> > [email protected]
>>>>> > https://lists.sourceforge.net/lists/listinfo/saxon-help
>>>>> >
>>>>> >
>>>>> >
>>>>> > _______________________________________________
>>>>> > saxon-help mailing list archived at http://saxon.markmail.org/
>>>>> > [email protected]
>>>>> > https://lists.sourceforge.net/lists/listinfo/saxon-help
>>>>> >
>>>>> >
>>>>> > _______________________________________________
>>>>> > saxon-help mailing list archived at http://saxon.markmail.org/
>>>>> > [email protected]
>>>>> > https://lists.sourceforge.net/lists/listinfo/saxon-help
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> saxon-help mailing list archived at http://saxon.markmail.org/
>>>>> [email protected]
>>>>> https://lists.sourceforge.net/lists/listinfo/saxon-help
>>>>
>>>> _______________________________________________
>>>> saxon-help mailing list archived at http://saxon.markmail.org/
>>>> [email protected]
>>>> https://lists.sourceforge.net/lists/listinfo/saxon-help
>>>
>>> _______________________________________________
>>> saxon-help mailing list archived at http://saxon.markmail.org/
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/saxon-help
>>
>> _______________________________________________
>> saxon-help mailing list archived at http://saxon.markmail.org/
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/saxon-help
>
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/saxon-help

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[email protected]
https://lists.sourceforge.net/lists/listinfo/saxon-help
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.