Re: Does XProc 3 allow me to call public function from XSLT 3 stylesheet?

On 08.07.2021 13:37, Martin Honnen wrote:

> XSLT 3 basically allows three ways to use a stylesheet, apply-templates
> with an initial mode, call-template with an initial template or invoking
> a public function with arguments
> (https://www.w3.org/TR/xslt-30/#invoking-initial-function).
>
> p:xslt in XProc 3 with the options for initial mode or template-name
> seems to only cater for apply-templates or call-template, but I miss a
> way there to call a public function.

So I hacked one, writing a library
https://github.com/martin-honnen/martin-honnen.github.io/blob/master/XProc3/libraries/call-function-in-XSLT-lib.xpl
which does

<p:library xmlns:p="http://www.w3.org/ns/xproc" version="3.0"
  xmlns:mf="http://example.com/mf"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <p:declare-step type="mf:call-function">

    <p:option name="xslt-lib-uri" as="xs:anyURI" required="true"/>

    <p:option name="function-name" as="xs:QName" required="true"/>
    <p:option name="function-arity" as="xs:integer" required="true"/>
    <p:option name="function-arguments" as="array(*)" select="[]"/>

    <p:input port="source"/>
    <p:output port="result" sequence="true"/>

    <p:xslt version="3.0" template-name="xsl:initial-template"
parameters="map { 'function-name' : $function-name, 'function-arity' :
$function-arity, 'function-arguments' : $function-arguments }"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <p:with-input port="stylesheet">
        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:mf="http://example.com/mf"
          exclude-result-prefixes="#all">

          <xsl:import href="{$xslt-lib-uri}"/>

          <xsl:param name="function-name" as="xs:QName" required="yes"/>
          <xsl:param name="function-arity" as="xs:integer" required="yes"/>
          <xsl:param name="function-arguments" as="array(*)" select="[]"/>

          <xsl:output build-tree="no"/>

          <xsl:template name="xsl:initial-template">
            <xsl:sequence select="function-lookup($function-name,
$function-arity) => apply($function-arguments)"/>
          </xsl:template>

        </xsl:stylesheet>
      </p:with-input>
    </p:xslt>

   </p:declare-step>

</p:library>

and can then be used as in
https://github.com/martin-honnen/martin-honnen.github.io/blob/master/XProc3/call-XSLT-function-test.xpl,
doing e.g.

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0"
     xmlns:mf="http://example.com/mf">
     <p:import href="libraries/call-function-in-XSLT-lib.xpl"/>
     <p:input port="source"/>
     <p:output port="result" sequence="true" serialization="map {
'method' : 'json' }"/>
     <mf:call-function
xslt-lib-uri="../../xslt/generic-positional-grouping-functions.xsl"
function-name="mf:group-into-sequence-of-arrays" function-arity="2"
function-arguments="[/root/item, 3]"/>
</p:declare-step>

Received on Thursday, 8 July 2021 18:38:25 UTC