Namespaces and variables both need to be declared (on the XPathCompiler) before the XPath expression is compiled. You can reuse the XPathCompiler to compile many XPath expressions, and its state (including declared variables and namespaces) is retained between compilations.
Value for variables need to be set up on the XPathSelector before the expression is evaluated. You can reuse the XPathSelector (and it retains the variable values) provided that you don't try to use it concurrently in different threads.
Hope this answers the question!
Michael Kay

From: [] On Behalf Of
Sent: 15 July 2008 15:26
Subject: [saxon] S9APIXpath and namespaces and variables

 I am trying to improve performance of my app by using the saxon API on JDOM documents rather than JAXEN.
  In the process I have a number of questions?
  1) Do I need to explicitly set up namespace context?
  2) If I do when should I do it?  Can it be done before the load of the compiled xpath?
  3) When do I set up variables? Can I do this before the load()?
  The reason for wanting to do this before the load is that I want to reduce the amount of setup for each new XPATH as I have literally hundreds to process against the same document with the same namespace and variable contexts?