blob: b670d58f3f01214960508aa238e10559267d23a4 [file] [log] [blame]
Armin Rigo9ed73062005-12-14 18:10:45 +00001#
2# ElementTree
Florent Xiclunaf15351d2010-03-13 23:24:31 +00003# $Id: ElementPath.py 3375 2008-02-13 08:05:08Z fredrik $
Armin Rigo9ed73062005-12-14 18:10:45 +00004#
5# limited xpath support for element trees
6#
7# history:
8# 2003-05-23 fl created
9# 2003-05-28 fl added support for // etc
10# 2003-08-27 fl fixed parsing of periods in element names
Florent Xiclunaf15351d2010-03-13 23:24:31 +000011# 2007-09-10 fl new selection engine
12# 2007-09-12 fl fixed parent selector
13# 2007-09-13 fl added iterfind; changed findall to return a list
14# 2007-11-30 fl added namespaces support
15# 2009-10-30 fl added child element value filter
Armin Rigo9ed73062005-12-14 18:10:45 +000016#
Florent Xiclunaf15351d2010-03-13 23:24:31 +000017# Copyright (c) 2003-2009 by Fredrik Lundh. All rights reserved.
Armin Rigo9ed73062005-12-14 18:10:45 +000018#
19# fredrik@pythonware.com
20# http://www.pythonware.com
21#
22# --------------------------------------------------------------------
23# The ElementTree toolkit is
24#
Florent Xiclunaf15351d2010-03-13 23:24:31 +000025# Copyright (c) 1999-2009 by Fredrik Lundh
Armin Rigo9ed73062005-12-14 18:10:45 +000026#
27# By obtaining, using, and/or copying this software and/or its
28# associated documentation, you agree that you have read, understood,
29# and will comply with the following terms and conditions:
30#
31# Permission to use, copy, modify, and distribute this software and
32# its associated documentation for any purpose and without fee is
33# hereby granted, provided that the above copyright notice appears in
34# all copies, and that both that copyright notice and this permission
35# notice appear in supporting documentation, and that the name of
36# Secret Labs AB or the author not be used in advertising or publicity
37# pertaining to distribution of the software without specific, written
38# prior permission.
39#
40# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
41# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
42# ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
43# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
44# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
45# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
47# OF THIS SOFTWARE.
48# --------------------------------------------------------------------
49
Fredrik Lundh63168a52005-12-14 22:29:34 +000050# Licensed to PSF under a Contributor Agreement.
Florent Xiclunaf15351d2010-03-13 23:24:31 +000051# See http://www.python.org/psf/license for licensing details.
Fredrik Lundh63168a52005-12-14 22:29:34 +000052
Armin Rigo9ed73062005-12-14 18:10:45 +000053##
54# Implementation module for XPath support. There's usually no reason
55# to import this module directly; the <b>ElementTree</b> does this for
56# you, if needed.
57##
58
59import re
60
Florent Xiclunaf15351d2010-03-13 23:24:31 +000061xpath_tokenizer_re = re.compile(
R David Murray44b548d2016-09-08 13:59:53 -040062 r"("
63 r"'[^']*'|\"[^\"]*\"|"
64 r"::|"
65 r"//?|"
66 r"\.\.|"
67 r"\(\)|"
68 r"[/.*:\[\]\(\)@=])|"
69 r"((?:\{[^}]+\})?[^/\[\]\(\)@=\s]+)|"
70 r"\s+"
Florent Xiclunaf15351d2010-03-13 23:24:31 +000071 )
Armin Rigo9ed73062005-12-14 18:10:45 +000072
Florent Xiclunaf15351d2010-03-13 23:24:31 +000073def xpath_tokenizer(pattern, namespaces=None):
Stefan Behnele8113f52019-04-18 19:05:03 +020074 default_namespace = namespaces.get('') if namespaces else None
Florent Xiclunaf15351d2010-03-13 23:24:31 +000075 for token in xpath_tokenizer_re.findall(pattern):
76 tag = token[1]
Stefan Behnele9927e12019-04-14 10:09:09 +020077 if tag and tag[0] != "{":
78 if ":" in tag:
Florent Xiclunaf15351d2010-03-13 23:24:31 +000079 prefix, uri = tag.split(":", 1)
Stefan Behnele9927e12019-04-14 10:09:09 +020080 try:
81 if not namespaces:
82 raise KeyError
83 yield token[0], "{%s}%s" % (namespaces[prefix], uri)
84 except KeyError:
85 raise SyntaxError("prefix %r not found in prefix map" % prefix) from None
86 elif default_namespace:
87 yield token[0], "{%s}%s" % (default_namespace, tag)
88 else:
89 yield token
Florent Xiclunaf15351d2010-03-13 23:24:31 +000090 else:
91 yield token
92
93def get_parent_map(context):
94 parent_map = context.parent_map
95 if parent_map is None:
96 context.parent_map = parent_map = {}
97 for p in context.root.iter():
98 for e in p:
99 parent_map[e] = p
100 return parent_map
101
102def prepare_child(next, token):
103 tag = token[1]
104 def select(context, result):
105 for elem in result:
106 for e in elem:
107 if e.tag == tag:
108 yield e
109 return select
110
111def prepare_star(next, token):
112 def select(context, result):
113 for elem in result:
Philip Jenveyfd0d3e52012-10-01 15:34:31 -0700114 yield from elem
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000115 return select
116
117def prepare_self(next, token):
118 def select(context, result):
Philip Jenveyfd0d3e52012-10-01 15:34:31 -0700119 yield from result
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000120 return select
121
122def prepare_descendant(next, token):
Raymond Hettinger828d9322014-11-22 21:56:23 -0800123 try:
124 token = next()
125 except StopIteration:
126 return
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000127 if token[0] == "*":
128 tag = "*"
129 elif not token[0]:
130 tag = token[1]
131 else:
132 raise SyntaxError("invalid descendant")
133 def select(context, result):
134 for elem in result:
135 for e in elem.iter(tag):
136 if e is not elem:
137 yield e
138 return select
139
140def prepare_parent(next, token):
141 def select(context, result):
142 # FIXME: raise error if .. is applied at toplevel?
143 parent_map = get_parent_map(context)
144 result_map = {}
145 for elem in result:
146 if elem in parent_map:
147 parent = parent_map[elem]
148 if parent not in result_map:
149 result_map[parent] = None
150 yield parent
151 return select
152
153def prepare_predicate(next, token):
154 # FIXME: replace with real parser!!! refs:
155 # http://effbot.org/zone/simple-iterator-parser.htm
156 # http://javascript.crockford.com/tdop/tdop.html
157 signature = []
158 predicate = []
159 while 1:
Raymond Hettinger828d9322014-11-22 21:56:23 -0800160 try:
161 token = next()
162 except StopIteration:
163 return
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000164 if token[0] == "]":
165 break
scoder101a5e82017-09-30 15:35:21 +0200166 if token == ('', ''):
167 # ignore whitespace
168 continue
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000169 if token[0] and token[0][:1] in "'\"":
170 token = "'", token[0][1:-1]
171 signature.append(token[0] or "-")
172 predicate.append(token[1])
173 signature = "".join(signature)
174 # use signature to determine predicate type
175 if signature == "@-":
176 # [@attribute] predicate
177 key = predicate[1]
178 def select(context, result):
179 for elem in result:
180 if elem.get(key) is not None:
181 yield elem
182 return select
183 if signature == "@-='":
184 # [@attribute='value']
185 key = predicate[1]
186 value = predicate[-1]
187 def select(context, result):
188 for elem in result:
189 if elem.get(key) == value:
190 yield elem
191 return select
R David Murray44b548d2016-09-08 13:59:53 -0400192 if signature == "-" and not re.match(r"\-?\d+$", predicate[0]):
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000193 # [tag]
194 tag = predicate[0]
195 def select(context, result):
196 for elem in result:
197 if elem.find(tag) is not None:
198 yield elem
199 return select
scoder101a5e82017-09-30 15:35:21 +0200200 if signature == ".='" or (signature == "-='" and not re.match(r"\-?\d+$", predicate[0])):
201 # [.='value'] or [tag='value']
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000202 tag = predicate[0]
203 value = predicate[-1]
scoder101a5e82017-09-30 15:35:21 +0200204 if tag:
205 def select(context, result):
206 for elem in result:
207 for e in elem.findall(tag):
208 if "".join(e.itertext()) == value:
209 yield elem
210 break
211 else:
212 def select(context, result):
213 for elem in result:
214 if "".join(elem.itertext()) == value:
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000215 yield elem
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000216 return select
217 if signature == "-" or signature == "-()" or signature == "-()-":
218 # [index] or [last()] or [last()-index]
219 if signature == "-":
Eli Bendersky5c6198b2013-01-24 06:29:26 -0800220 # [index]
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000221 index = int(predicate[0]) - 1
Eli Bendersky5c6198b2013-01-24 06:29:26 -0800222 if index < 0:
223 raise SyntaxError("XPath position >= 1 expected")
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000224 else:
225 if predicate[0] != "last":
226 raise SyntaxError("unsupported function")
227 if signature == "-()-":
Armin Rigo9ed73062005-12-14 18:10:45 +0000228 try:
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000229 index = int(predicate[2]) - 1
230 except ValueError:
231 raise SyntaxError("unsupported expression")
Eli Bendersky5c6198b2013-01-24 06:29:26 -0800232 if index > -2:
233 raise SyntaxError("XPath offset from last() must be negative")
Armin Rigo9ed73062005-12-14 18:10:45 +0000234 else:
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000235 index = -1
236 def select(context, result):
237 parent_map = get_parent_map(context)
238 for elem in result:
239 try:
240 parent = parent_map[elem]
241 # FIXME: what if the selector is "*" ?
242 elems = list(parent.findall(elem.tag))
243 if elems[index] is elem:
244 yield elem
245 except (IndexError, KeyError):
246 pass
247 return select
248 raise SyntaxError("invalid predicate")
249
250ops = {
251 "": prepare_child,
252 "*": prepare_star,
253 ".": prepare_self,
254 "..": prepare_parent,
255 "//": prepare_descendant,
256 "[": prepare_predicate,
257 }
Armin Rigo9ed73062005-12-14 18:10:45 +0000258
259_cache = {}
260
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000261class _SelectorContext:
262 parent_map = None
263 def __init__(self, root):
264 self.root = root
Armin Rigo9ed73062005-12-14 18:10:45 +0000265
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000266# --------------------------------------------------------------------
267
268##
269# Generate all matching objects.
270
271def iterfind(elem, path, namespaces=None):
272 # compile selector pattern
273 if path[-1:] == "/":
274 path = path + "*" # implicit all (FIXME: keep this?)
Stefan Behnele9927e12019-04-14 10:09:09 +0200275
276 cache_key = (path,)
277 if namespaces:
Stefan Behnele8113f52019-04-18 19:05:03 +0200278 cache_key += tuple(sorted(namespaces.items()))
Stefan Behnele9927e12019-04-14 10:09:09 +0200279
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000280 try:
Eli Bendersky2acc5252013-08-03 17:47:47 -0700281 selector = _cache[cache_key]
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000282 except KeyError:
283 if len(_cache) > 100:
284 _cache.clear()
285 if path[:1] == "/":
286 raise SyntaxError("cannot use absolute path on element")
287 next = iter(xpath_tokenizer(path, namespaces)).__next__
Raymond Hettinger828d9322014-11-22 21:56:23 -0800288 try:
289 token = next()
290 except StopIteration:
291 return
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000292 selector = []
293 while 1:
294 try:
295 selector.append(ops[token[0]](next, token))
296 except StopIteration:
Pablo Galindo0df19052017-10-16 09:24:22 +0100297 raise SyntaxError("invalid path") from None
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000298 try:
299 token = next()
300 if token[0] == "/":
301 token = next()
302 except StopIteration:
303 break
Eli Bendersky2acc5252013-08-03 17:47:47 -0700304 _cache[cache_key] = selector
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000305 # execute selector pattern
306 result = [elem]
307 context = _SelectorContext(elem)
308 for select in selector:
309 result = select(context, result)
310 return result
Armin Rigo9ed73062005-12-14 18:10:45 +0000311
312##
313# Find first matching object.
314
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000315def find(elem, path, namespaces=None):
Raymond Hettinger0badfd52014-11-28 14:52:14 -0800316 return next(iterfind(elem, path, namespaces), None)
Armin Rigo9ed73062005-12-14 18:10:45 +0000317
318##
319# Find all matching objects.
320
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000321def findall(elem, path, namespaces=None):
322 return list(iterfind(elem, path, namespaces))
323
324##
325# Find text for first matching object.
326
327def findtext(elem, path, default=None, namespaces=None):
328 try:
329 elem = next(iterfind(elem, path, namespaces))
330 return elem.text or ""
331 except StopIteration:
332 return default