<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: util.xml 17299 2012-10-10 23:47:33Z joewiz $ --><book><bookinfo><productname>eXist-db – Open Source Native XML Database</productname><title>The util module</title><orgname>The eXist-db Project</orgname><date>October 2012</date></bookinfo><chapter><title>The util module</title><section id="introduction"><title>Introduction</title><para>The util module (function namespace <uri>http://exist-db.org/xquery/util</uri>)
                contains a number of common utility functions, such as <command>util:md5</command>,
                which can generate md5 hashes. The full list of functions and their documentation is
                in the <ulink url="/exist/apps/xqfdocs">Function Documentation Library</ulink>. This
                article discusses some of the highlights and main uses for this module.</para></section><section id="eval"><title>util:eval()</title><variablelist><varlistentry><term>util:eval()</term><listitem><para>This function is used to dynamically execute a constructed XQuery
                            expression inside a running XQuery script. This can be very handy in
                            some cases - for example, web-based applications that dynamically
                            generate queries based on HTTP request parameters the user has
                            passed.</para><para>By default, the dynamically executed query inherits most of the
                            current context, including local variables:</para><synopsis language="xquery">
let $a := "Hello"
return util:eval("$a")
                            </synopsis></listitem></varlistentry></variablelist><para>Consider the following simple example script in which any two numbers submitted by
                a user are added or subtracted: </para><example><title>Adding/Subtracting Two Numbers</title><programlisting language="xquery">
xquery version "1.0";

declare namespace request="http://exist-db.org/xquery/request";
declare namespace util="http://exist-db.org/xquery/util";

declare function local:do-query() as element() {
    let $n1 := request:get-parameter("n1", "")
	let $n2 := request:get-parameter("n2", "")
	let $op := request:get-parameter("op", "")
	return
		if($n1 = "" or $n2 = "") then
			&lt;p&gt;Please enter two operands.&lt;/p&gt;
		else
			let $query := concat($n1, " ", $op, " ", $n2)
			return
				&lt;p&gt;{$query} = {util:eval($query)}&lt;/p&gt;
};

&lt;html&gt;
	&lt;body&gt;
		&lt;h1&gt;Enter two numbers&lt;/h1&gt;

		&lt;form action="{request:get-uri()}" method="get"&gt;
			&lt;table border="0" cellpadding="5"&gt;
			&lt;tr&gt;
				&lt;td&gt;First number:&lt;/td&gt;
				&lt;td&gt;&lt;input name="n1" size="4"/&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td&gt;Operator:&lt;/td&gt;
				&lt;td&gt;
					&lt;select name="op"&gt;
						&lt;option name="+"&gt;+&lt;/option&gt;
						&lt;option name="-"&gt;-&lt;/option&gt;
					&lt;/select&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td&gt;Second number:&lt;/td&gt;
				&lt;td&gt;&lt;input name="n2" size="4"/&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td colspan="2"&gt;&lt;input type="submit"/&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;/table&gt;
		&lt;/form&gt;

		{ local:do-query() }
		
	&lt;/body&gt;
&lt;/html&gt;</programlisting></example><para>In this example, there is one XQuery script responsible for evaluating the
                user-supplied parameters, which uses the parameters from the HTTP request to
                construct another XQuery expression, which it then passes to
                    <command>util:eval</command> for evaluation. The application would then
                post-process the returned results, and display them to the user. (For more
                information on how to write web applications using XQuery, go to our <ulink url="devguide.xml">Developer's Guide</ulink>.)</para></section></chapter></book>