# $Id: ElementPath.py 3375 2008-02-13 08:05:08Z fredrik $
# limited xpath support for element trees
# 2003-05-28 fl added support for // etc
# 2003-08-27 fl fixed parsing of periods in element names
# 2007-09-10 fl new selection engine
# 2007-09-12 fl fixed parent selector
# 2007-09-13 fl added iterfind; changed findall to return a list
# 2007-11-30 fl added namespaces support
# 2009-10-30 fl added child element value filter
# Copyright (c) 2003-2009 by Fredrik Lundh. All rights reserved.
# http://www.pythonware.com
# --------------------------------------------------------------------
# The ElementTree toolkit is
# Copyright (c) 1999-2009 by Fredrik Lundh
# By obtaining, using, and/or copying this software and/or its
# associated documentation, you agree that you have read, understood,
# and will comply with the following terms and conditions:
# Permission to use, copy, modify, and distribute this software and
# its associated documentation for any purpose and without fee is
# hereby granted, provided that the above copyright notice appears in
# all copies, and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of
# Secret Labs AB or the author not be used in advertising or publicity
# pertaining to distribution of the software without specific, written
# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
# ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# --------------------------------------------------------------------
# Licensed to PSF under a Contributor Agreement.
# See http://www.python.org/psf/license for licensing details.
# Implementation module for XPath support. There's usually no reason
# to import this module directly; the <b>ElementTree</b> does this for
xpath_tokenizer_re = re.compile(
r"((?:\{[^}]+\})?[^/\[\]\(\)@=\s]+)|"
def xpath_tokenizer(pattern, namespaces=None):
for token in xpath_tokenizer_re.findall(pattern):
if tag and tag[0] != "{" and ":" in tag:
prefix, uri = tag.split(":", 1)
yield token[0], "{%s}%s" % (namespaces[prefix], uri)
raise SyntaxError("prefix %r not found in prefix map" % prefix)
def get_parent_map(context):
parent_map = context.parent_map
context.parent_map = parent_map = {}
for p in context.root.iter():
def prepare_child(next, token):
def select(context, result):
def prepare_star(next, token):
def select(context, result):
def prepare_self(next, token):
def select(context, result):
def prepare_descendant(next, token):
raise SyntaxError("invalid descendant")
def select(context, result):
def prepare_parent(next, token):
def select(context, result):
# FIXME: raise error if .. is applied at toplevel?
parent_map = get_parent_map(context)
parent = parent_map[elem]
if parent not in result_map:
result_map[parent] = None
def prepare_predicate(next, token):
# FIXME: replace with real parser!!! refs:
# http://effbot.org/zone/simple-iterator-parser.htm
# http://javascript.crockford.com/tdop/tdop.html
if token[0] and token[0][:1] in "'\"":
token = "'", token[0][1:-1]
signature.append(token[0] or "-")
predicate.append(token[1])
signature = "".join(signature)
# use signature to determine predicate type
def select(context, result):
if elem.get(key) is not None:
def select(context, result):
if elem.get(key) == value:
if signature == "-" and not re.match(r"\-?\d+$", predicate[0]):
def select(context, result):
if elem.find(tag) is not None:
if signature == "-='" and not re.match(r"\-?\d+$", predicate[0]):
def select(context, result):
for e in elem.findall(tag):
if "".join(e.itertext()) == value:
if signature == "-" or signature == "-()" or signature == "-()-":
# [index] or [last()] or [last()-index]
index = int(predicate[0]) - 1
raise SyntaxError("XPath position >= 1 expected")
if predicate[0] != "last":
raise SyntaxError("unsupported function")
index = int(predicate[2]) - 1
raise SyntaxError("unsupported expression")
raise SyntaxError("XPath offset from last() must be negative")
def select(context, result):
parent_map = get_parent_map(context)
parent = parent_map[elem]
# FIXME: what if the selector is "*" ?
elems = list(parent.findall(elem.tag))
except (IndexError, KeyError):
raise SyntaxError("invalid predicate")
"//": prepare_descendant,
def __init__(self, root):
# --------------------------------------------------------------------
# Generate all matching objects.
def iterfind(elem, path, namespaces=None):
# compile selector pattern
cache_key = (path, None if namespaces is None
else tuple(sorted(namespaces.items())))
path = path + "*" # implicit all (FIXME: keep this?)
selector = _cache[cache_key]
raise SyntaxError("cannot use absolute path on element")
next = iter(xpath_tokenizer(path, namespaces)).__next__
selector.append(ops[token[0]](next, token))
raise SyntaxError("invalid path")
_cache[cache_key] = selector
# execute selector pattern
context = _SelectorContext(elem)
result = select(context, result)
# Find first matching object.
def find(elem, path, namespaces=None):
return next(iterfind(elem, path, namespaces), None)
# Find all matching objects.
def findall(elem, path, namespaces=None):
return list(iterfind(elem, path, namespaces))
# Find text for first matching object.
def findtext(elem, path, default=None, namespaces=None):
elem = next(iterfind(elem, path, namespaces))