Hi Dirk,

Cool! Exactly what I was looking for, and it executes fast as well.
This is a life-saver for me.

Could you elaborate a bit on the "Using the empty string for the binding binds the element as context item (thus it must be a document)." remark?
I'm not sure if I follow that...
And what are the other binding options?

Thanks a lot.

Paul
Hi Paul,

xquery:eval() should be capable of doing what you want. Take a look at
the first example at https://docs.basex.org/wiki/XQuery_Module

Using the empty string for the binding binds the element as context item
(thus it must be a document). So the following should work for you:

   xquery:eval($choice, map { '': document {$xml}})

Cheers,
Dirk

On 10/08/14 21:15, Paul Swennenhuis wrote:
Suppose I have a variable $xml containing the XML fragment
<places><village>Wismar</village><city>Amsterdam</city><village>Positano
village, Italy</village><city>Prague</city></places>
In a static world I would list all cities with the XPath expression
$xml//city .
And the villages with $xml//village .

OK. No issues. Runs fast.

Now suppose I want the user to decide whether to list either cities or
villages.
There are several ways to implement this, but I'm looking for the
dynamic XPath solution.

Suppose the users choice ("city" or "village") resides in the string
$choice
Something like $xml/$choice abviously will not work. It will yield the
string "village" or "city" 8 times (for all element and text nodes)
xquery:eval() does not help either. xquery:eval($xml/$choice) is not
valid, nor is $xml/xquery:eval($choice) or any variant of these, with or
without parentheses or curly braces.

One way I have found to do it is like this:

xquery:eval(concat(fn:serialize($xml),$choice))

Complete query:

let $xml :=
<places><village>Wismar</village><city>Amsterdam</city><village>Positano
village, Italy</village><city>Prague</city></places>

let $choice := '//village'

return
xquery:eval(concat(fn:serialize($xml),$choice))


But obviously, this executes very slowly because the XML has to be
serialized.

So is there any other way to do dynamic joining of XML fragments and
XPath expressions given as string?

Oh, BTW, I am NOT looking for this solution:
$xml//*[local-name()=$choice]
Thiis may work for this case but not for other XPath expressions.



Reply via email to