Jump to content

XQuery/Manipulating URIs

From Wikibooks, open books for an open world

Motivation

[edit | edit source]

Sometimes you need to be able to manipulate the URI of your own XQuery. This is useful when you need to call your own XQuery with different parameters. For example if you have an XQuery that returns the first 20 rows in a query but you want to add a Get Next 20 Records button you may want to simply call yourself with additional parameters for what record to start with, in this case start at record 21.

This program demonstrates some XQuery functions that are not part of the original XQuery specification but are required for precise web server XQuery functionality.

The functions are:

  • eXist request:get-uri() - Returns the URI of the current request within the web server. For example

/exist/rest/db/test/my-query.xq

  • eXist request:get-url() - Returns the full URL including the server and port.

http://www.example.com:8080/exist/rest/db/test/my-query.xq

  • eXist request:get-query-string() - Returns the full query string passed to the servlet (without the initial question mark).
  • eXist system:get-module-load-path() - returns the path to the place where a module has been loaded from
  • eXist system:get-exist-home() - returns the base of the eXist web root

Sample Program

[edit | edit source]
xquery version "1.0";
declare namespace system="http://exist-db.org/xquery/system";
declare namespace request="http://exist-db.org/xquery/request";
declare option exist:serialize "method=html media-type=text/html indent=yes";

let $get-uri := request:get-uri()
let $get-url := request:get-url()
let $module-load-path := system:get-module-load-path()
let $exist-home := system:get-exist-home()
let $path := substring-after($module-load-path, 'xmldb:exist://embedded-eXist-server')
let $replace := replace($module-load-path, 'xmldb:exist://embedded-eXist-server', '')

return
<html>
   <head>
   <title>URI Path Example</title>
   </head>
   <body>
   <h1>Sample URI manipulation with XPath</h1>
      <table border="1">
         <thead>
           <tr>             
             <th>In</th>
             <th>Out</th>
           </tr>
         </thead>
         <tr>
            <td>request:get-url()</td>
            <td>{$get-url}</td>
         </tr>
         <tr>
            <td>request:get-uri()</td>
            <td>{$get-uri}</td>
         </tr>
         <tr>
             <td>system:get-module-load-path()</td>
             <td>{$module-load-path}</td>
         </tr>
         <tr>
             <td>system:get-exist-home()</td>
             <td>{$exist-home}</td>
         </tr>
         <tr>
            <td>substring-after(system:get-module-load-path(), 'xmldb:exist://embedded-eXist-server')</td>
            <td>{$path}</td></tr>
         <tr>
            <td>replace(system:get-module-load-path(), 'xmldb:exist://embedded-eXist-server', '')</td>
            <td>{$replace}</td>
         </tr>        
    </table>
  </body>
</html>