0
votes

I want to add a custom xpath extension function to the Saxon-HE transformer. This custom function should accept one or more arguments. Let's use the string concatenation analogy for concatenating one or more string arguments. Following the sample on the saxon page, i wrote the following code:

ExtensionFunction myconcat = new ExtensionFunction() {
  public QName getName() {
      return new QName("http://mycompany.com/", "myconcat");
  }

  public SequenceType getResultType() {
      return SequenceType.makeSequenceType(
          ItemType.STRING, OccurrenceIndicator.ONE
      );
  }

  public net.sf.saxon.s9api.SequenceType[] getArgumentTypes() {
      return new SequenceType[]{
          SequenceType.makeSequenceType(
              ItemType.STRING, OccurrenceIndicator.ONE_OR_MORE)};
  }

  public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
      //concatenate the strings here....
      String result = "concatenated string";          
      return new XdmAtomicValue(result);
  }
};

i have expected that the following xpath expression would work in an xsl file

<xsl:value-of select="myconcat('a','b','c','...')">

Unfortunately i got the following exception:

XPST0017: Function myconcat must have 1 argument

What is the right way of creating a custom function for this use case?

Thanks.

1
Such arguments are called varargs and Saxon apparently does not support those. Possible duplicate question.predi
Perhaps but it seemed to me a reflection based extension question.ilhami visne

1 Answers

1
votes

The standard mechanisms for creating extension functions don't allow a variable number of arguments (it's not really pukka to have such functions in the XPath view of the world - concat() is very much an exception).

You can do it by creating your own implementation of the class FunctionLibrary and adding your FunctionLibrary to the static context of the XSLT engine - but you're deep into Saxon internals if you attempt that, so be prepared for a rough ride.