root/bin/simplify_xpath @ 1123
1 | 33 | aaronmk | #!/usr/bin/env python
|
---|---|---|---|
2 | # Removes duplication from XPath expressions
|
||
3 | 59 | aaronmk | # Filters one XPath per line from stdin to stdout
|
4 | 33 | aaronmk | |
5 | import re |
||
6 | import sys |
||
7 | |||
8 | def main(): |
||
9 | while True: |
||
10 | line = sys.stdin.readline() |
||
11 | if line == '': break |
||
12 | # Forward * abbrs
|
||
13 | 611 | aaronmk | line = re.sub(r'(\w+)(?=\w*(?:->/[^/]*)?/\1\b)', r'*', line) |
14 | 33 | aaronmk | # Backward * abbrs
|
15 | 611 | aaronmk | line = re.sub(r'((\w+)->/[^/]*/[^/]*\[)\2', r'\1*', line) |
16 | 33 | aaronmk | sys.stdout.write(line) |
17 | |||
18 | main() |