Question

I use Saxon-He v.9.5.1-2 for XSLT and I need a custom Java function that would let me return multiple values (preferably map of values) in one call. Is this possible to achieve?

I've been thinking about extending net.sf.saxon.lib.ExtensionFunctionDefinition class like this:

public class GetSth extends ExtensionFunctionDefinition {

    @Override
    public StructuredQName getFunctionQName() {
        return new StructuredQName("bf", "http://some.url.com/", "get-sth");
    }

    @Override
    public SequenceType[] getArgumentTypes() {
        return new SequenceType[]{SequenceType.ATOMIC_SEQUENCE};
    }

    @Override
    public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
        return SequenceType.NODE_SEQUENCE;
    }

    @Override
    public ExtensionFunctionCall makeCallExpression() {
        return new ExtensionFunctionCall() {
            @Override
            public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
                Map<String, String> map = <my_map_here>;
                /*
                    How to return multiple values? I would expect to return some class from net.sf.saxon.value package.
                    return ?
                */                  
            }
        };
    }
}

If it's not possible for a map, how to return a list then? Please advise.

Was it helpful?

Solution

Maps aren't supported in Saxon-HE, so you will have to find some other representation. You could of course construct an XML document of some kind, or you could return a list of strings in which the key/value pairs are represented as a single string in a microsyntax such as "key:value" which you then parse out in the XSLT code. For example:

List<StringValue> list = new ArrayList<StringValue>();
for (MapEntry m : map.entrySet()) {
  list.add(new StringValue(m.getKey() + ":" + m.getValue());
}
return new SequenceExtent(list);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top