If you're interested in serializing Saxon Expression trees, you might want to check out https://github.com/msokolov/lux/blob/master/src/main/java/lux/compiler/SaxonTranslator.java; this class converts Saxon Expressions into an alternate expression tree representation that in turn serializes (via toString()) to executable XQuery (1.0).  Every effort has been made to preserve the semantics, and as much as possible the form, of the original expression, even to the extent of undoing a few of Saxon's optimizations.  There are a few known bugs (last I checked we had 20-odd failures out of nearly 20,000 test cases in the XQuery Test Suite).

It's probably true that problems will arise with newer versions of Saxon, since as Michael Kay says, this code relies on undocumented internal APIs, but at least moving from 9.4 to 9.5 only caused minimal disruption.

-Mike

On 3/31/14 12:04 PM, Michael Kay wrote:

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
Saxonica


On 31 Mar 2014, at 16:54, Denis DEBARBIEUX <denis.debarbieux@inria.fr> 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.
Denis
------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help



------------------------------------------------------------------------------


_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help