3

I'm writing xquery on eXist.

Usually I use this way to select item in xml:

fn:doc($document_name)/root/a

But now I wants to get the xpath from a string variable:

let $xpath := request:get-parameter("xpath", "")
fn:doc($document_name)/$xpath

Of course it doesn't work. The only way I found now is using eval:

util:eval(fn:concat("fn:doc($document_name)", $xpath)):)

but i don't want to use eval because it's slow and not safe.

I know there's something like:

fn:doc($document_name)/*[name()='node_name']

but I want to select item via the whole path but not only the name of node and I also have tried to use node-xpath() but don't know how to use it just like name()

Joe Wicentowski
  • 5,159
  • 16
  • 26
owenwater
  • 811
  • 1
  • 6
  • 18

2 Answers2

5

You want to do what the eval() function does, so any solution is going to have the same problems as eval. The other approach you could consider is generating a query and then executing it, but it will have exactly the same problems. If you think it might be safer to restrict the string to a subset of XPath expressions (e.g. with no predicates, or no function calls) then you could try testing for those conditions using simple regular expressions.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

despite Michael Kay being right, maybe the functx:dynamic-path() is of some help.

It might be a good intermediate solution sitting between fn:eval and generating the query dynamically.

Hope this helps

Michael

michael
  • 1,577
  • 1
  • 12
  • 18
  • I'm not sure whether it's a good idea or not, but at least eXist doesn't support this function now. – owenwater Dec 01 '11 at 00:55
  • @owen_water You need to include the functx library. See: http://www.xqueryfunctions.com/xq/functx-1.0-doc-2007-01.xq – wst Jan 17 '14 at 19:16