I'm using Saxon-HE 9.2.
The javadoc for the XQueryCompiler class says that a single compiler "may be used repeatedly to compile multiple queries".
When running an XQuery that includes a library module like [1], it works fine the first time, but attempts to re-use the compiler to compile another query apparently result in the function from the imported library not being resolved.

  XPST0017: Cannot find a matching 1-argument function named
  {http://xsparql.deri.org/XSPARQLer/xsparql.xquery}_serialize()
; SystemID: *module with no systemId*; Line#: 11; Column#: -1
net.sf.saxon.trans.XPathException: Cannot find a matching 1-argument function named {http://xsparql.deri.org/XSPARQLer/xsparql.xquery}_serialize()
at net.sf.saxon.query.UnboundFunctionLibrary.bindUnboundFunctionCalls(UnboundFunctionLibrary.java:114)
at net.sf.saxon.query.QueryModule.bindUnboundFunctionCalls(QueryModule.ja..

An example of the setup I'm using for this invocation is up at [2].

Re-use of the compiler works for queries that don't import libraries.

In an attempt to work around this, I was trying XQueryCompiler.compileLibrary(File query) function, but that seems to always give syntax errors on libraries, like:

Error on line 1 column 0 of xsparql.xquery:
  XPST0003: XQuery syntax error in #module namespace _xsparql#:
    Module declaration must not be used in a main module
Error on line 3 column 18 of xsparql.xquery:
  XPST0081: XQuery static error in #... function _xsparql:_rdf_term($#:
    Prefix _xsparql has not been declared
Error on line 4 column 1 of xsparql.xquery:
  XPST0003: XQuery syntax error in #...ing) as xs:string { $input };#:
    Unexpected token "<eof>" in path expression
; SystemID: file:/tmp/xsparql.xquery; Line#: 4; Column#: 1
net.sf.saxon.trans.XPathException: XQuery syntax error in #...ing) as xs:string { $input };#:
    Unexpected token "<eof>" in path expression
at net.sf.saxon.query.QueryParser.grumble(QueryParser.java:360)
at net.sf.saxon.expr.ExpressionParser.grumble(ExpressionParser.java:138)
at net.sf.saxon.expr.ExpressionParser.grumble(...

Then I gave XQueryCompiler.compileLibrary(String query) a try, and it tells me that feature is only available on Saxon-EE.

I suppose for now the workaround for queries that import libraries is to create a new compiler for every query?
-Leif

[1] http://gist.github.com/514799
[2] http://github.com/LeifW/MusicPath/blob/master/src/main/scala/org/musicpath/XQueryCall.scala