qt4cg / qtspecs

QT4 specifications
https://qt4cg.org/
Other
28 stars 15 forks source link

Support for inline (anonymous) xslt functions #745

Closed MarkNicholls closed 4 months ago

MarkNicholls commented 1 year ago

I propose adding support for inline xslt functions.

Whilst XPath supports this, Xpath functions are limited in what they can do, and how "look" e.g. returning newly constructed elements isnt possible without parse-xml-fragment.

I would suggest the syntax would be basically the same as for xsl:function except with the name omitted, e.g.

    <xsl:template name="apply-function" as="xs:integer">
        <xsl:param name="input" as="xs:integer"/>
        <xsl:param name="function" as="function(xs:integer) as xs:integer"/>
        <xsl:sequence select="$function($input)"/>
    </xsl:template>

`    <xsl:template ....>
        <xsl:call-template name="apply-function">
            <xsl:with-param name="input" select="1"/>
            <xsl:with-param name="function">
                <xsl:function as="xs:integer">
                    <xsl:param name="value" as="xs:integer"/>
                    <result>
                        <xsl:sequence select="$value * 2"/>
                    </result>
                </xsl:function>
            </xsl:with-param>
        </xsl:call-template>
    </xsl:template>
`

benefits

alternatives

michaelhkay commented 1 year ago

This is very closely related to issue #735 (almost a duplicate). The difference is that this issue proposes the local function should be anonymous (so that the local xsl:function element is effectively an instruction that returns a function item); whereas my proposal was that the local function should be named, and added to the static context. I can see the merits in both, and the temptation is to combine them. But perhaps that would create too much complexity?

MarkNicholls commented 1 year ago

If i assign as anonymous function to local variable, isn't that a local function? (barring syntax)

michaelhkay commented 1 year ago

If i assign as anonymous function to local variable, isn't that a local function? (barring syntax)

Not quite. Local variables aren't in scope within their own body, or within earlier instructions/declarations, so it's hard to do recursion with anonymous functions bound to variables, in the way that you can do with named functions. In addition (in 4.0) named functions have some additional capabilities such as defaulted parameter values.

MarkNicholls commented 1 year ago

ah, its not until you point it out that you realise the nuances. I'm used to functions just being values and only in scope for subsequent instructions, does that mean they effectively precede all instructions in the scope? and thus can't form closures (except from params)? Is this a problem?

michaelhkay commented 1 year ago

Global xsl:function declarations in XSLT are in scope throughout the stylesheet, including within their own bodies, and within other functions declared earlier. They have access to global variables, but global variables are immutable, so they don't need to go in any kind of closure.

ndw commented 5 months ago

At the F2F in Prague, group consensus was towards named, inline functions (See #735)

ndw commented 4 months ago

The CG agreed to close this issue without further action at meeting 081