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.