8.4. Adding XML Search Features to xgrep

The current code is a good start on the road to an XML-aware grep, but there are a number of improvements we can make quite easily. Now seems as good a time as any to add them. We will allow xgrep to do the following.

  • Use a validating source of XML events (i.e., xmlv)

  • Match on attribute names

  • Match on attribute values

  • Match on processing instructions

  • Match on element type names

Adding these to the code is a lot easier than dreaming up a sensible way to specify them with single-letter command options! Thankfully, Python's getopt library supports the use of multicharacter command options that are preceded with two rather than one hyphen. Here are the new options we will add.

  • --V Turn on validation

  • --AN Match ...

Get XML Processing with Python now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.