if (starts-with($path, "/eXide/execute"))
then
  let $query := request:get-parameter("qu", ())
  let $startTime := util:system-time()
  return
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
      <!-- Query is executed by XQueryServlet -->
      <forward servlet="XQueryServlet">
        <!-- Query is passed via the attribute "xquery.source" -->
        <set-attribute name="xquery.source" value="{$query}"/>
        <!-- Results should be written into attribute "results" -->
        <set-attribute name="xquery.attribute" value="results"/>
        <!-- Errors should be passed through instead of terminating the request -->
        <set-attribute name="xquery.report-errors" value="yes"/>
      </forward>
      <view>
        <!-- Post process the result: store it into the HTTP session and return the number of hits only. -->
        <forward url="session.xq">
          <clear-attribute name="xquery.source"/>
          <clear-attribute name="xquery.attribute"/>
          <set-attribute name="elapsed" value="{string(seconds-from-duration(util:system-time() - $startTime))}"/>
        </forward>
      </view>
    </dispatch>
else
  if (starts-with($path, "/sandbox/results/"))
  then
    (: Retrieve an item from the query results stored in the HTTP session. The
      format of the URL will be /sandbox/results/X, where X is the number of the
      item in the result set :)
    <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
      <forward url="../session.xq">
        <add-parameter name="num" value="{$name}"/>
      </forward>
    </dispatch>
