def string_value(obj): if is_tag_node(obj) or is_attribute_node(obj) or is_text_node(obj): return derive_text_from_node(obj, peek_context().preserve_space) elif is_number(obj) or is_boolean(obj): return str(obj) elif is_node_set(obj): return string_value(obj[0]) if len(obj) > 0 else '' elif is_sequence(obj): return ''.join(string_value(item) for item in obj) elif is_string(obj): return obj else: raise NotImplementedError('string_value not implemented for type "{0}"'.format(obj.__class__.__name__))
def accept_context_node(): context = peek_context() format_str = u'Evaluating predicate expression for context node at position {0} of {1}: {2}.' verbose_print(lambda: format_str.format(context.position, context.size, debug_dump_node(context.node))) value = expression_fn() if is_number(value): accept = number(context.position) == value else: accept = bool(value) verbose_print(lambda: u'{0} node {1}'.format('Accepted' if accept else 'Rejected', debug_dump_node(context.node))) return [context.node] if accept else []
def odd(): return boolean(peek_context().position % 2 == 1)
def even(): return boolean(peek_context().position % 2 == 0)
def position(): return number(peek_context().position)
def last(): return number(peek_context().size)