The output of Expression.toString() is intended only for diagnostics. You should not attempt to do anything with the output other than display it.

In 3.0 there is a better chance of generating valid syntax because of the introduction of the Q{uri}local notation for QNames; without this, anything that's generated is either invalid syntax, or syntax whose meaning depends on the namespace context. The original prefixes aren't retained in the compiled expression and therefore can't be faithfully reproduced. 

The optimizer may generate constructs such as "let" expressions which aren't available in XPath 2.0. There's also a possibility that the generated output has insufficient parentheses to capture the operator precedence correctly.

The compiled expression may also include function calls to internal functions that aren't available to user-written applications.

If you want access to the expression tree, you can either navigate the tree directly, or write your own ExpressionPresenter to receive information about it. Either approach, though, is vulnerable to changes in future releases; this isn't a stable interface.

Michael Kay

On 31 Mar 2014, at 16:54, Denis DEBARBIEUX <> wrote:

Dear all,

I am building an XPath expression using Saxon and the net.sf.saxon.expr.Expression class.

When I call the toString method on my objects, I get an XPath 3.0 query. Is-it possible to generate an XPath 2.0 query?

Moreover, the documentation contains a warning: there is no guarantee that the syntax will actually be true XPath.

Is this case common or only for rare corner cases?

Thanks for your help.
saxon-help mailing list archived at