f:make-ref, f:is-ref, and f:QName added
parent
b384e5fad1
commit
33788871a9
|
@ -1,4 +1,4 @@
|
||||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<!--
|
<!--
|
||||||
Dynamic function application
|
Dynamic function application
|
||||||
|
|
||||||
|
@ -195,4 +195,78 @@
|
||||||
</message>
|
</message>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Create a reference to dynamic function @var{name} with arity
|
||||||
|
@var{arity}
|
||||||
|
|
||||||
|
This function @emph{does not} verify that the function @var{name}
|
||||||
|
exists, nor does it verify that the provided @var{arity} is valid
|
||||||
|
for it. Further, the returned function reference will work
|
||||||
|
@emph{only with dynamic functions}—that is, an application template
|
||||||
|
is needed. See @file{transform/apply-gen.xsl} for more information
|
||||||
|
and examples.
|
||||||
|
-->
|
||||||
|
<function name="f:make-ref" as="element( f:ref )">
|
||||||
|
<param name="name" as="xs:QName" />
|
||||||
|
<param name="arity" as="xs:integer" />
|
||||||
|
|
||||||
|
<variable name="ns"
|
||||||
|
select="namespace-uri-from-QName( $name )" />
|
||||||
|
|
||||||
|
<f:ref arity="{$arity}">
|
||||||
|
<element name="{$name}"
|
||||||
|
namespace="{$ns}" />
|
||||||
|
</f:ref>
|
||||||
|
</function>
|
||||||
|
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Determines whether @var{fnref} represents a valid dynamic function
|
||||||
|
reference
|
||||||
|
|
||||||
|
This can be used to determine if @var{fnref} is valid as input to
|
||||||
|
other functions, some of which may produce an error if called with
|
||||||
|
an invalid dynamic function reference.
|
||||||
|
|
||||||
|
@i{Implementation details:} To be valid, @var{fnref} must:
|
||||||
|
|
||||||
|
@enumerate
|
||||||
|
@item Be an element of type @code{f:ref};
|
||||||
|
@item Have a numeric @code{@arity}; and
|
||||||
|
@item Have a child target function node.
|
||||||
|
@enumerate
|
||||||
|
-->
|
||||||
|
<function name="f:is-ref" as="xs:boolean">
|
||||||
|
<param name="fnref" as="item()*" />
|
||||||
|
|
||||||
|
<variable name="ref" select="$fnref[ 1 ]" />
|
||||||
|
|
||||||
|
<!-- for @arity check: note that NaN != NaN -->
|
||||||
|
<sequence select="$ref instance of element( f:ref )
|
||||||
|
and number( $ref/@arity ) = number( $ref/@arity )
|
||||||
|
and exists( $ref/*[ 1 ] )" />
|
||||||
|
</function>
|
||||||
|
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Retrieve the QName of the target dynamic function
|
||||||
|
|
||||||
|
Usually, this would match precisely the QName of the target
|
||||||
|
function.
|
||||||
|
|
||||||
|
@i{Implementation details:} This actually represents the QName of
|
||||||
|
the @emph{application template}, which could differ from the target
|
||||||
|
function name. One reason this may be the case is to provide a
|
||||||
|
function alias.
|
||||||
|
-->
|
||||||
|
<function name="f:QName" as="xs:QName?">
|
||||||
|
<param name="fnref" as="element( f:ref )" />
|
||||||
|
|
||||||
|
<variable name="target" as="element()?"
|
||||||
|
select="$fnref/element()[ 1 ]" />
|
||||||
|
|
||||||
|
<sequence select="node-name( $target )" />
|
||||||
|
</function>
|
||||||
|
|
||||||
</stylesheet>
|
</stylesheet>
|
||||||
|
|
|
@ -28,13 +28,25 @@
|
||||||
<import href="../src/apply.xsl" />
|
<import href="../src/apply.xsl" />
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn0 ]">
|
<!-- catch errors (otherwise, we'd terminate) -->
|
||||||
|
<template mode="f:apply"
|
||||||
|
match="*"
|
||||||
|
priority="2">
|
||||||
|
<foo:unknown-dyn-fun />
|
||||||
|
</template>
|
||||||
|
|
||||||
|
|
||||||
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn0 ]"
|
||||||
|
priority="5">
|
||||||
<!-- return something to show that we were called properly -->
|
<!-- return something to show that we were called properly -->
|
||||||
<foo:applied n="0" />
|
<foo:applied n="0" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn1 ]">
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn1 ]"
|
||||||
|
priority="5">
|
||||||
<param name="arg1" />
|
<param name="arg1" />
|
||||||
|
|
||||||
<foo:applied n="1" />
|
<foo:applied n="1" />
|
||||||
|
@ -42,7 +54,9 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn2 ]">
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn2 ]"
|
||||||
|
priority="5">
|
||||||
<param name="arg1" />
|
<param name="arg1" />
|
||||||
<param name="arg2" />
|
<param name="arg2" />
|
||||||
|
|
||||||
|
@ -51,7 +65,9 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn3 ]">
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn3 ]"
|
||||||
|
priority="5">
|
||||||
<param name="arg1" />
|
<param name="arg1" />
|
||||||
<param name="arg2" />
|
<param name="arg2" />
|
||||||
<param name="arg3" />
|
<param name="arg3" />
|
||||||
|
@ -61,7 +77,9 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn4 ]">
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn4 ]"
|
||||||
|
priority="5">
|
||||||
<param name="arg1" />
|
<param name="arg1" />
|
||||||
<param name="arg2" />
|
<param name="arg2" />
|
||||||
<param name="arg3" />
|
<param name="arg3" />
|
||||||
|
@ -72,7 +90,9 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn5 ]">
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn5 ]"
|
||||||
|
priority="5">
|
||||||
<param name="arg1" />
|
<param name="arg1" />
|
||||||
<param name="arg2" />
|
<param name="arg2" />
|
||||||
<param name="arg3" />
|
<param name="arg3" />
|
||||||
|
@ -85,7 +105,9 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn6 ]">
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn6 ]"
|
||||||
|
priority="5">
|
||||||
<param name="arg1" />
|
<param name="arg1" />
|
||||||
<param name="arg2" />
|
<param name="arg2" />
|
||||||
<param name="arg3" />
|
<param name="arg3" />
|
||||||
|
@ -99,7 +121,9 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn7 ]">
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn7 ]"
|
||||||
|
priority="5">
|
||||||
<param name="arg1" />
|
<param name="arg1" />
|
||||||
<param name="arg2" />
|
<param name="arg2" />
|
||||||
<param name="arg3" />
|
<param name="arg3" />
|
||||||
|
@ -114,7 +138,9 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<template mode="f:apply" match="f:ref[ foo:fn8 ]">
|
<template mode="f:apply"
|
||||||
|
match="f:ref[ foo:fn8 ]"
|
||||||
|
priority="5">
|
||||||
<param name="arg1" />
|
<param name="arg1" />
|
||||||
<param name="arg2" />
|
<param name="arg2" />
|
||||||
<param name="arg3" />
|
<param name="arg3" />
|
||||||
|
|
161
test/apply.xspec
161
test/apply.xspec
|
@ -26,6 +26,9 @@
|
||||||
xmlns:foo="http://www.lovullo.com/_junk"
|
xmlns:foo="http://www.lovullo.com/_junk"
|
||||||
stylesheet="apply-test.xsl">
|
stylesheet="apply-test.xsl">
|
||||||
|
|
||||||
|
<variable name="foo-uri"
|
||||||
|
select="namespace-uri-for-prefix( 'foo', $args )" />
|
||||||
|
|
||||||
<variable name="args">
|
<variable name="args">
|
||||||
<foo:args>
|
<foo:args>
|
||||||
<foo:arg1 />
|
<foo:arg1 />
|
||||||
|
@ -222,4 +225,162 @@
|
||||||
</scenario>
|
</scenario>
|
||||||
</scenario>
|
</scenario>
|
||||||
</scenario>
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="f:make-ref constructor">
|
||||||
|
<scenario label="when called with a function QName and arity">
|
||||||
|
<variable name="qname"
|
||||||
|
select="QName( $foo-uri, 'reftest' )" />
|
||||||
|
<variable name="arity"
|
||||||
|
select="2" />
|
||||||
|
|
||||||
|
<call function="f:make-ref">
|
||||||
|
<param name="name" select="$qname" />
|
||||||
|
<param name="arity" select="$arity" />
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="produces a dynamic function reference"
|
||||||
|
test="f:is-ref( $x:result )" />
|
||||||
|
|
||||||
|
<expect label="produces provided reference arity"
|
||||||
|
test="f:arity( $x:result ) = $arity" />
|
||||||
|
|
||||||
|
<expect label="produces reference to expected QName"
|
||||||
|
test="f:QName( $x:result ) = $qname" />
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="produces a valid dynamic function reference">
|
||||||
|
<call function="f:apply">
|
||||||
|
<param name="fnref"
|
||||||
|
select="f:make-ref( QName( $foo-uri, 'fn1' ),
|
||||||
|
1 )" />
|
||||||
|
<param name="arg1"
|
||||||
|
select="$args/foo:arg1" />
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="that can be applied using f:apply"
|
||||||
|
test="$x:result[ 1 ] = foo:applied[ @n = 1 ]
|
||||||
|
and $x:result[ 2 ] is $args/foo:arg1" />
|
||||||
|
</scenario>
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="f:is-ref predicate">
|
||||||
|
<scenario label="given a valid dynamic function reference">
|
||||||
|
<call function="f:is-ref">
|
||||||
|
<param name="fnref"
|
||||||
|
select="f:make-ref( QName( $foo-uri, 'foo' ), 0 )" />
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns true()"
|
||||||
|
select="true()" />
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="given an invalid dynamic function reference">
|
||||||
|
<call function="f:is-ref">
|
||||||
|
<param name="fnref">
|
||||||
|
<foo:bar />
|
||||||
|
</param>
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns false()"
|
||||||
|
select="false()" />
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="given a dynamic function reference without arity">
|
||||||
|
<call function="f:is-ref">
|
||||||
|
<param name="fnref">
|
||||||
|
<f:ref>
|
||||||
|
<foo:bar />
|
||||||
|
</f:ref>
|
||||||
|
</param>
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns false()"
|
||||||
|
select="false()" />
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="given a dynamic function reference without
|
||||||
|
integral arity">
|
||||||
|
<call function="f:is-ref">
|
||||||
|
<param name="fnref">
|
||||||
|
<f:ref arity="moo">
|
||||||
|
<foo:bar />
|
||||||
|
</f:ref>
|
||||||
|
</param>
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns false()"
|
||||||
|
select="false()" />
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="given a dynamic function reference without target
|
||||||
|
node">
|
||||||
|
<call function="f:is-ref">
|
||||||
|
<param name="fnref">
|
||||||
|
<f:ref arity="0" />
|
||||||
|
</param>
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns false()"
|
||||||
|
select="false()" />
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="given an empty sequence">
|
||||||
|
<call function="f:is-ref">
|
||||||
|
<param name="fnref" select="()" />
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns false()"
|
||||||
|
select="false()" />
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="given a non-element">
|
||||||
|
<call function="f:is-ref">
|
||||||
|
<param name="fnref" select="5" />
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns false()"
|
||||||
|
select="false()" />
|
||||||
|
</scenario>
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="f:QName accessor">
|
||||||
|
<scenario label="given a valid dynamic function reference">
|
||||||
|
<variable name="foo-qname"
|
||||||
|
select="QName( $foo-uri, 'foo' )" />
|
||||||
|
|
||||||
|
<call function="f:QName">
|
||||||
|
<param name="fnref"
|
||||||
|
select="f:make-ref( $foo-qname, 0 )" />
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns QName"
|
||||||
|
test="$x:result instance of xs:QName" />
|
||||||
|
|
||||||
|
<expect label="returns QName of target dynamic function"
|
||||||
|
select="$foo-qname" />
|
||||||
|
</scenario>
|
||||||
|
|
||||||
|
|
||||||
|
<scenario label="given a dynamic function reference with no target
|
||||||
|
node">
|
||||||
|
<call function="f:QName">
|
||||||
|
<param name="fnref">
|
||||||
|
<f:ref arity="5" />
|
||||||
|
</param>
|
||||||
|
</call>
|
||||||
|
|
||||||
|
<expect label="returns empty sequence"
|
||||||
|
select="()" />
|
||||||
|
</scenario>
|
||||||
|
</scenario>
|
||||||
</description>
|
</description>
|
||||||
|
|
Loading…
Reference in New Issue