diff options
author | scoder <stefan_ml@behnel.de> | 2017-09-30 13:35:21 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-09-30 13:35:21 (GMT) |
commit | 101a5e84acbab9d880e150195f23185dfb5449a9 (patch) | |
tree | 9fd7915a7e4049f33f9d083312d2c1b78d4eeb38 /Lib/xml | |
parent | 9811e80fd0ed9d74c76a66f1dd4e4b8afa9e8f53 (diff) | |
download | cpython-101a5e84acbab9d880e150195f23185dfb5449a9.zip cpython-101a5e84acbab9d880e150195f23185dfb5449a9.tar.gz cpython-101a5e84acbab9d880e150195f23185dfb5449a9.tar.bz2 |
bpo-31648: Improve ElementPath (#3835)
* Allow whitespace inside of ElementPath predicates.
* Add ElementPath predicate support for text comparison of the current node, like "[.='text']".
Diffstat (limited to 'Lib/xml')
-rw-r--r-- | Lib/xml/etree/ElementPath.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/Lib/xml/etree/ElementPath.py b/Lib/xml/etree/ElementPath.py index 361f6d5..c9d6ef3 100644 --- a/Lib/xml/etree/ElementPath.py +++ b/Lib/xml/etree/ElementPath.py @@ -157,6 +157,9 @@ def prepare_predicate(next, token): return if token[0] == "]": break + if token == ('', ''): + # ignore whitespace + continue if token[0] and token[0][:1] in "'\"": token = "'", token[0][1:-1] signature.append(token[0] or "-") @@ -188,16 +191,22 @@ def prepare_predicate(next, token): if elem.find(tag) is not None: yield elem return select - if signature == "-='" and not re.match(r"\-?\d+$", predicate[0]): - # [tag='value'] + if signature == ".='" or (signature == "-='" and not re.match(r"\-?\d+$", predicate[0])): + # [.='value'] or [tag='value'] tag = predicate[0] value = predicate[-1] - def select(context, result): - for elem in result: - for e in elem.findall(tag): - if "".join(e.itertext()) == value: + if tag: + def select(context, result): + for elem in result: + for e in elem.findall(tag): + if "".join(e.itertext()) == value: + yield elem + break + else: + def select(context, result): + for elem in result: + if "".join(elem.itertext()) == value: yield elem - break return select if signature == "-" or signature == "-()" or signature == "-()-": # [index] or [last()] or [last()-index] |