Search code examples
javaxpathxsltsaxon

How do I get declared namespaces from the net.sf.saxon.expr.XPathContext in a Java extension function


In analogy to this question: How do I get declared variables

I am writing code for a Java extension function and need to find the namespace URI for a given prefix from the namespace declarations in the XSL. I got as far as context.getNamePool(), but I cannot figure out how to use the returned net.sf.saxon.om.NamePool (Saxon 9).


Solution

  • The in-scope namespaces of a function call (or any other expression) are part of the static context of that expression, which is not directly available via Saxon's XPathContext object (this contains the dynamic context only).

    You don't say which API you are using to define your extension functions (see https://www.saxonica.com/documentation12/index.html#!extensibility/extension-functions-J):

    • With the reflexive interface, the static context isn't available to the body of the function.

    • Similarly, with the simple interface, the static context isn't available.

    • With the full interface, that is, with extension functions defined using the s9api method Processor.registerExtensionFunction(ExtensionFunctionDefinition), Saxon at compile time calls ExtensionFunctionDefinition.makeCallExpression to create the ExtensionFunctionCall object, and then calls ExtensionFunctionCall.supplyStaticContext() to supply the static context information including the namespace bindings.

    Using this information is complicated by the fact that in general, the StaticContext object can resolve a prefix to a namespace, but it can't enumerate all the prefixes that have a defined binding. That's a consequence of the fact that Saxon supports the JAXP mechanism for binding extension functions in XPath, which uses the very badly designed Java interface javax.xml.namespace.NamespaceContext.

    This is nothing to do with the NamePool, by the way. The NamePool holds information about the totality of names in use within a Configuration, and holds nothing specific to the context of an individual expression.