Wednesday, 4 Nov 2009
This tool dumps document fragments matched by an XPath expression from an XML document. It’s handy for shell script based manipulation of XML.
usage: xpathgrep [-w pfx:tag] [-n pfx:uri] [-e xpath] file.xml [...] -w Name of document element to wrap all results; if you do not use a prefix, the element will be in the default namespace. If a wrapper is not requested, the results will be given individually. -n Namespace to register for use in XPath; syntax is prefix, followed by the namespace URI, separated by a semicolon, eg. ``html:http://www.w3.org/1999/xhtml'' to bind the URI ``http://www.w3.org/1999/xhtml'' to the prefix ``html''. If you have requested a wrapper, all registered namespaces will also be declared on it. To declare a default namespace on the wrapper, register a namespace with an empty prefix, eg ``-e :http://www.w3.org/1999/xhtml''. -e An XPath expression; will be evaluated against each document. For a single expression you can omit the switch and just give it before any filenames. -h This help. You can use a filename of ``-'' to denote standard input. Note that your output will probably have lots of redundant namespace declarations; libxml2 offers no easy way to clean them up at the time of writing. If you want to clean up a result document, pipe it to ``xmllint --nsclean -''.
The older version 0.2 is useful as a condensed example of using the libxml2 XPath API in C.
Changes in 0.3:
- The command line interface changed completely. The utility is now actually useful for more than the simplest of use cases.