Perform XPath Queries inside Ant
May 17th, 2009
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.
- Download the latest version of XmlTask from http://xmltask.sf.net
- Create a task definition in Ant
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="./xmltask-v1.15.1.jar" />
- 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[1]" 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.
