Apache Ant/Execute an XQuery

Motivation Edit

You want to execute an XQuery that is stored in an eXist database.

Remote execution of an inline query Edit

<target name="run-one-inline-test-local">
        <description>Execute a single xUnit test on a local system</description>
        <echo message="Run an inline XQuery"/>
        <xdb:xquery uri="xmldb:exist://localhost/xmlrpc/db" user="${user}" password="${password}"      
        xquery version "1.0";
        let $message := 'Hello World!'
        return $message
        <echo message="Result = ${result}"/>

Note that you only can return a string in this example. Any XML content in the query will generate an error.

If you want to return an XML file into a property you will need to wrap you query in a CDATA structure:

 <!-- This version uses CDATA to put an XML file into the result property -->
<target name="run-xquery-cdata">
        <xdb:xquery user="admin" password="" uri="${test-server}/db" outputproperty="result"><![CDATA[
            xquery version "1.0";
            let $message := 'Hello World'
        <echo message="Result = ${result}"/>

Execute an XQuery Stored in Local Drive Edit


xquery version "1.0";
let $message := 'Hello World'

This is similar to the version above but you will note that the queryfile attribute has been added.

<target name="run-in-database-query" depends="load-test-resources">
    <xdb:xquery user="${user}" password="${password}"
       uri="xmldb:exist://localhost/xmlrpc/db" queryfile="hello-world.xq"
    <echo message="Result = ${result}"/>

Note for the above to work the file hello-word.xq MUST be in the same directory as the build script.

Adding Execute Permissions Edit

<target name="add-execute">
    <!-- make the controller.xql file executable -->
    <xdb:chmod uri="${local-uri}/apps/myapp" resource="controller.xql" permissions="group=+execute,other=+execute"/>

Where the local-uri is something like: xmldb:exist://localhost:8080/exist/xmlrpc/db for the default installation path