Perform XPath Queries inside Ant

Ant offers some XML features like the xslt task, which transforms XML files into other files. But unfortunately, with Ant you can’t make simple XPath Queries on a specific XML file.


Luckily, there is a nice open source solution called XmlTask, which offers many possibilities to work with XML files.


  1. Download the latest version of XmlTask from
  2. Create a task definition in Ant

    <taskdef name="xmltask" 
      classpath="./xmltask-v1.15.1.jar" />
  3. Use it with the <xmltask> definition

    For example, read out the number of a specific node in the XML file:

    <xmltask source="someFile.xml">
      <copy path="count(//aNode)" property="numberOfNodes" />

    Or change an attribute:

    <xmltask source="someFile.xml" dest="toAnotherFile.xml">
      <attr path="//aNode&#91;1&#93;" attr="enabled" value="true"/>


There are many other possibilites to work with XML files inside Ant. Check out the documentation at

About the author


Add comment

Recent Posts