Apache Ant/Execute an XQuery
Appearance
Motivation
[edit | edit source]You want to execute an XQuery that is stored in an eXist database.
Remote execution of an inline query
[edit | edit source]<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}"
outputproperty="result">
xquery version "1.0";
let $message := 'Hello World!'
return $message
</xdb:xquery>
<echo message="Result = ${result}"/>
</target>
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'
return
<result>{$message}</result>
]]></xdb:xquery>
<echo message="Result = ${result}"/>
</target>
Execute an XQuery Stored in Local Drive
[edit | edit source]hello-world.xq:
xquery version "1.0";
let $message := 'Hello World'
return
<result>{$message}</result>
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"
outputproperty="result"/>
<echo message="Result = ${result}"/>
</target>
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 | edit source]<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"/>
</target>
Where the local-uri is something like: xmldb:exist://localhost:8080/exist/xmlrpc/db for the default installation path