M ChangeLog.md +6 -0
@@ 1,3 1,9 @@
+2019-08-27:
+
+ * add option `-r` (`--regex`)
+ * bump version to 2.4
+
+
2019-08-24:
* add option `-q` (`--quiet`)
M README.md +7 -4
@@ 28,6 28,7 @@ Usage:
[-m] [--matches]
[-n] [--declare-ns]
[-q] [--quiet]
+ [-r <ns>] [--regex <ns>]
<XPath expression>
<XML file(s)>
@@ 36,10 37,12 @@ Usage:
xgrep.py [-h] [--help]
Normally, `xgrep.py` outputs the matching parts of the XML files together with
-their filenames and the XPath expression. The option `-m` outputs only the
-matching parts, without filenames or XPath expressions. The option `-i` indents
-the matching parts, and the option `-n` includes name-space declarations. The
-options `-c`, `-l`, `-L`, and `-q` mimic the behaviour of
+their filenames and the XPath expression. If `-r <ns>` is set, the
+[EXSLT function `<ns>:test`](http://exslt.org/regexp/functions/test/) can be
+used in the XPath expression for matching regular expressions. The option `-m`
+outputs only the matching parts, without filenames or XPath expressions. The
+option `-i` indents the matching parts, and the option `-n` includes namespace
+declarations. The options `-c`, `-l`, `-L`, and `-q` mimic the behaviour of
[GNU grep](http://www.gnu.org/software/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).
M xgrep.py +7 -2
@@ 1,6 1,6 @@
#! /usr/bin/python3
# xgrep.py -- search for elements in XML files, using XPath 1.0 expressions
-# Andreas Nolda 2019-08-24
+# Andreas Nolda 2019-08-27
import sys
import argparse
@@ 8,7 8,7 @@ import re
from blessings import Terminal
from lxml import etree
-version=2.3
+version=2.4
term = Terminal()
@@ 31,6 31,8 @@ parser.add_argument("-n", "--declare-ns"
help="declare namespaces in matches")
parser.add_argument("-q", "--quiet", action="store_true",
help="only return exit status")
+parser.add_argument("-r", "--regex", metavar="ns", nargs=1,
+ help="namespace prefix for EXSLT regular expressions")
parser.add_argument("-v", "--version", action="version",
version="{0} {1}".format(parser.prog, version))
args = parser.parse_args()
@@ 120,6 122,9 @@ def main():
else "default":value
for key,value in root.nsmap.items()}
nsexpr = insert_default_ns(args.expr, nsmap)
+ # add regexp namespace *after* inserting default namespace
+ if args.regex:
+ nsmap[args.regex[0]] = "http://exslt.org/regular-expressions"
matches = tree.xpath(nsexpr, namespaces=nsmap)
if not args.quiet:
if args.files_without_match: