Jump to content

Apache Ant/XQuery

From Wikibooks, open books for an open world

Motivation

[edit | edit source]

You want to transform an XML document with XQuery using an ant task.

Method

[edit | edit source]

We will use the Saxon library to demonstrate this.

Steps:

  1. Download the Saxon library from Sourceforge
  2. Download a sample XQuery from the samples (for example tour.xq from the samples area)
  3. Copy the Saxon jar file into your project. In the example below just a single jar file is copied into the location saxonhe9-2-0-6j/saxon9he.jar

Sample Ant Target

[edit | edit source]

This sample uses the java task to run an XQuery program using the Saxon Java library. In the example below the XQuery tour.xq is executed and the output is copied into the file output.html.

Note that the starting point is set by passing the arg as a parameter to the XQuery.

<target name="run-saxon-xquery">
    <java classname="net.sf.saxon.Query" output="output.html">
             <arg value="tour.xq"/>
             <classpath>
                <pathelement location="saxonhe9-2-0-6j/saxon9he.jar"/>
            </classpath>
             <arg value="start=e5"/> 	
     </java>
    <!-- On Windows, this will open FireFox after the Transform is done -->
    <exec command="C:\Program Files\Mozilla Firefox\firefox.exe
    	C:\ws\Saxon-Test\output.html"/>
</target>