introduce ElementTraversal.collect and simplify RewriteRules that can
make use of the new method