def evaluate(self, context): if self.prefix == None: node_set = [context.node] else: node_set = self.prefix.evaluate(context) assert is_node_set(node_set) return do_step(node_set, self.axis, self.node_test, self.predicate_list, context)
def evaluate(self, context): if self.op == 'or': if to_boolean(self.left.evaluate(context)): return True return to_boolean(self.right.evaluate(context)) elif self.op == 'and': if not to_boolean(self.left.evaluate(context)): return False return to_boolean(self.right.evaluate(context)) elif self.op in ['=', '!=', '<', '>', '<=', '>=']: return compare(self.op, self.left.evaluate(context), self.right.evaluate(context)) elif self.op in ['+', '-', '*', 'div', 'mod']: x = to_number(self.left.evaluate(context)) y = to_number(self.right.evaluate(context)) if self.op == '+': return x + y elif self.op == '-': return x - y elif self.op == '*': return x * y elif self.op == 'div': return x / y elif self.op == 'mod': z = abs(x) % abs(y) if x >= 0: return z else: return -z else: assert False elif self.op == '|': x = self.left.evaluate(context) y = self.right.evaluate(context) if is_node_set(x) and is_node_set(y): # XXX This is incorrect, because it neither preserves # document order nor removes duplicates. return join_node_sets(x, y) else: raise XPathEvaluationError, "Operands of '|' must be node sets." else: assert False
def evaluate(self, element, document): if document is None: parent = element while parent is not None: document = parent parent = document.parent context = Context(document, element, self.namespace_mapping) result = self.parsed_xpath.evaluate(context) if is_node_set(result) and not isinstance(result, list): # Node sets can be all sorts of things internally, but # let's normalize them to lists at this point. result = list(result) return result