A Python script for searching XML files for patterns specified in terms of XPath expressions.
fix link to COPYING in README.md
change color of ellipsis in abbreviations
Added tag 2.9 for changeset 254dec1347bf

clone

read-only
https://hg.sr.ht/~nolda/xgrep
read/write
ssh://hg@hg.sr.ht/~nolda/xgrep

XGrep

xgrep.py is a Python 3 script for searching for elements in XML files, using XPath 1.0 expressions.

The script is released 'as is' with no warranty under the GNU General Public License, version 2.0.

It requires the following Python 3 packages:

The lxml package internally calls the libxml2 and libxslt C libraries.

On Debian-based systems, the prerequisites can be installed as follows:

sudo apt-get install python3-blessings python3-lxml

Usage:

xgrep.py [-a] [--abbreviate]
         [-c] [--count]
         [-C] [--force-color]
         [-i] [--indent]
         [-l] [--files-with-matches]
         [-L] [--files-without-match]
         [-m] [--matches]
         [-M] [--files-and-matches]
         [-n] [--line-number]
         [-N] [--declare-ns]
         [-q] [--quiet]
         [-r <ns>] [--regex <ns>]
         <XPath expression>
         <XML file(s)>

xgrep.py [-v] [--version]

xgrep.py [-h] [--help]

Normally, xgrep.py outputs the matching parts of the XML files together with their file names and the XPath expression. The option -m outputs only the matching parts, without file names or XPath expressions; with -M, the matching parts are prefixed with the corresponding file name. If -r <ns> is set, the EXSLT function <ns>:test() can be used in the XPath expression for matching regular expressions. The option -i indents the matching parts, and the option -N includes namespace declarations. Matching parts can be abbreviated to their first line by means of the option -a. The -C option preserves color and formatting codes when piping output through GNU less or similar programs.

The options -c, -l, -L, -n, and -q mimic the behaviour of GNU grep. The latter option suppresses any output, but still returns the exit status (0 if there are matches, 1 if there are none, and 2 for errors).

Andreas Nolda (andreas@nolda.org)