title: In XQuery, how to evaluate xpath from a string?
body: <p>I have an <code>XQuery</code> script that I would like to evaluate as I import new documents into a database. I want to evaluate an <code>Xpath</code> expression, with a predicate, from a string, and use the result to choose the appropriate collection. </p>
  
  <p>It works perfectly if all of the code is in the <code>XQuery</code> file. I want to make it more dynamic than that and read <code>XPath</code> <strong>expressions -> collection mappings dynamically</strong>. Then, in each document, when an <code>XPath</code> expression returns a node, I have found the correct collection.</p>
  
  <p>Is this possible? The <code>xdmp:unpath()</code> function isn't adequate. It doesn't support predicates.  <code>xdmp:eval()</code> and <code>xdmp:value()</code> seem to work but I can't get them to work only against a single document. Either that, or they just start to fail (returning true for any <code>XPath</code> expression) after 500 or so documents.</p>
  
  <p>I would like to be able to do something like this:</p>
  
  <pre><code>(: Get XML from MarkLogic Record Loader. :)
  let $doc := xdmp:unquote($XML-STRING, $UNQ-NAMESPACE, $UNQ-OPTIONS)
  
  (: Get XPath expression from database. :)
  let $xpath := doc("/mappings.xml")/mappings/rule/xpath
  
  (: Evaluate. :)
  let $result := some-function($doc, $xpath) 
  
  (: Use the result.
  return
      if(fn:count($result) &gt; 0)
          ( $collection )
      else
          ()   
  </code></pre>
  
  <p>Is there some construct that will make "some-function($doc, $xpath)" work?</p>
  
  <p>It seems that in <code>XQuery</code>, <code>XPath</code> expressions are part of the syntax and can't be replaced by strings. I'm used to other languages like Perl where <code>XPath</code> is always a string and something like this would be trivial. </p>
  
  <p>Thanks.</p>