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 http://xmltask.sf.net
  2. Create a task definition in Ant

    <taskdef name="xmltask" 
      classname="com.oopsconsultancy.xmltask.ant.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" />
    </xmltask>
    

    Or change an attribute:

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

 

There are many other possibilites to work with XML files inside Ant. Check out the documentation at http://xmltask.sf.net.

About the author

stefan.jaeger

Add comment

Recent Posts