fn:function-lookup on extension functions with optional arguments
Added by Martin Honnen over 2 years ago
I have managed to integrate the CoffeeSacks library into my XML Workbench using Saxon-HE 11.3 Java in the backend.
While trying to use the extension functions provided from pure XPath without any namespace declarations present I found that Saxon raises no error on e.g. the expression
function-lookup(QName('http://nineml.com/ns/coffeesacks', 'grammar-string'), 1)
for the extension function being declared in https://github.com/nineml/coffeesacks/blob/main/src/main/java/org/nineml/coffeesacks/GrammarStringFunction.java#L31 as having one or two arguments
public class GrammarStringFunction extends CommonDefinition {
private static final StructuredQName qName =
new StructuredQName("", "http://nineml.com/ns/coffeesacks", "grammar-string");
private URI baseURI = null;
public GrammarStringFunction(Configuration config, ParserCache cache) {
super(config, cache);
}
@Override
public StructuredQName getFunctionQName() {
return qName;
}
@Override
public int getMinimumNumberOfArguments() {
return 1;
}
@Override
public int getMaximumNumberOfArguments() {
return 2;
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[]{SequenceType.SINGLE_STRING, SequenceType.OPTIONAL_ITEM};
}
but in the end returns for that expression a function Q{http://nineml.com/ns/coffeesacks}grammar-string#2
, i.e. a function expecting two arguments, and consequently raises an error when trying to call the function with only one argument e.g.
function-lookup(QName('http://nineml.com/ns/coffeesacks', 'grammar-string'), 1)("s = 'a', 'b', a'.")
gives
Error(s) during XPath evaluation: Number of arguments required for dynamic call to grammar-string is 2; number supplied = 1
Is that a restriction of function-lookup
and dynamic function call with extension functions?
Or should function-lookup(QName('http://nineml.com/ns/coffeesacks', 'grammar-string'), 1)
give me the one argument version?
Or does the extension function need a different implementation to allow the use with function-lookup for both the two and the one argument version?
Replies (2)
RE: fn:function-lookup on extension functions with optional arguments - Added by Martin Honnen over 2 years ago
The example to call the function should be (to use a valid grammar)
function-lookup(QName('http://nineml.com/ns/coffeesacks', 'grammar-string'), 1)("s = 'a', 'b', 'a'.")
and not
function-lookup(QName('http://nineml.com/ns/coffeesacks', 'grammar-string'), 1)("s = 'a', 'b', a'.")
but the question about whether function-lookup(QName('http://nineml.com/ns/coffeesacks', 'grammar-string'), 1)
should return me e.g. the one argument version remains open.
RE: fn:function-lookup on extension functions with optional arguments - Added by Michael Kay over 2 years ago
Problem reproduced and logged as a bug here: https://saxonica.plan.io/issues/5587
Please register to reply