Manipulating XML files using Apache Ant

Recently I needed to work out large number of XML documents. I was positively surprised how much one can do using old good Apache Ant. In this post I present libraries that add a powerful XML support in it.

Using XSLT 2.0

When you think about manipulating XML transformation is main feature. Ant has inbuilt task to handle XSLT but bare installation handles version 1.0 only (based on Xalan). If you want to use powerful and much more convenient version 2.0 you must download Saxon processor. After you put it on the classpath it is used instead of standard one only when you add a factory tag inside <xslt> task invocation:

<factory name="net.sf.saxon.TransformerFactoryImpl"/>

Direct access to XML elements

Sometimes you would prefer access XML document directly instead of transformation. In that case xmltask library is really helpful. It supports insert, copy replace and remove operations based on XPath 1,0 expressions. Most interesting feature is iteration through tree elements and handling them separately.

Main disadvantage of this tool is no namespace support. When you access to an element with a prefix you must use lengthy *[local-name()='node-name'] expression.

XQuery and XML database

When number of XML files is significant or when you want to find complicated relations between documents then database must be used. Among free XML databases eXist is most mature and is equipped with dedicated ant tasks. These allows CRUD operations on resources as well as executing XPath and XQuery processing on database contents.

Simple webservice client

When everything was transformed, queried or extracted then maybe results should be submitted to a webservice? There is a simple library called Fikin Ant Tasks that (among other goods like XPath 1.0 evaluation) allows to execute most of standard HTTP methods. Most important and unique for me was multipart POST.

Need much more?

When above feature are not enough you should look at implementations of XProc standard. Maybe I would describe its features soon.

0 komentarze:

Post a Comment