[XQuery XPath] Dynamic XPath evaluation in an XQuery

Hi all,

I would like to know how to evaluate an XPath contained into a string
from an XQuery processor.

Here is a simple example of my requirement:

I have an XML witch conatins XPath expressions like this:

/objects/textobject/text

Hello world !


I know how to query some ‘iobj’ and would like to build a document
with the content referenced by the XPath expression. I have to do this
in an XQuery.

Of course the context in witch I need this kind of evaluation is more
complex.

I hope someone will be able to help me.

Thank you,
Emmanuel

This is something which the current XQuery specs don’t support. I suspect it’s not something we can expect, either. I pushed to get this facility into XSLT 2.0 (in which case it might have made it into XQuery), but other implementors voted it out.

I suspect something like this might find its way into the specs when XPointer becomes more accepted. The ability for an XQuery to follow inter-document links defined using XPointers is something that implementors would find it hard to argue against. Rather than using a direct XPath expression in your document, you would then have to use an XPointer. XPointer, of course, is based on XPath syntax.

Some XSLT products have an extension called xx:evaluate() which does exactly what you need, but there’s nothing similar in current XQuery products, or in Tamino. It’s potentially extremely expensive in performance terms, of course, because it makes it impossible to optimize the query in advance. It would be far less problematic if the XPath/XPointer in the source document were identified as such in the schema, which would mean it could be precompiled (and perhaps even pre-evaluated).

XSLT users generally get around the restriction using one of two techniques, which might also work with XQuery:

(a) in simple cases, path expressions of the form *[name()=$param] allow a limited degree of run-time parameterization of a query.

(b) failing that, you can generate the query from the application: e.g. write one query that reads the document and extracts the XPath expression, and use this to generate a second query which the application then submits to the database.

Michael Kay