xmltools: command-line tools for XML

[ download ] [ xml source ] [ discussion ]

This is a set of command-line tools for handling XML. Each of the tools performs a particular function to edit XML files, and they all work with locators, which are effectively URLs which identify specific pieces of an XML file. A locator consists of a series of element specifications separated by periods; each element specification either has a round-parenthetical numeric location or a square-bracketed ID location. So, for instance, if we have some XML of the form:

   <item id="first"/>
   <item id="second"/>

Then the locators thingy.item(1) and thingy.item[second] would each refer to the second item. If the items hadn't been empty, we could build a locator such as thingy.item(1).stuff, which would refer to the first "stuff" elements in the second item. (The default location is (0).)

So the following program embodies four actions: snip, replace, set, and insert. "Snip" simply returns the contents of the location named. "Replace" replaces the location with whatever's on stdin (no guarantee of well-formedness!) "Set" is used to set an attribute on the named elements, and "insert" inserts the content of stdin either before or after the named element, or before or after the named element's content.

A word of apology: although the four actions end up in four different executables, I have all four being generated from the same source code, with #defines to determine which of the four actions is to be taken. This makes for somewhat confusing code, but I hope that the literate programming presentation will make up for it.

The program uses James Clark's expat XML parser, which I highly recommend. It is documented in these pages in a literate style, which I highly recommend. The actual tool I'm using to generate the documentation pages is my tool LPML, which I can only recommend if you're really brave. It's working for me, but then I'm writing it myself.

The motivation for the development is the open-source workflow toolkit, which relies heavily on XML and expat. These tools will be used in the datasheet manipulation portion of the whole system.

Here's how we do all this stuff:

This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2000, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license.