xpath.Expression
¶
The xpath.Expression
class represents a valid, compiled xpath expression.
It can be instantiated with the xpath.Expression.compile()
classmethod.
The same xpath.Expression
can be used for multiple executions with different xpath.RuntimeOptions
.
There is also a utility function xpath.compile()
that can be used to create an xpath.Expression
.
session = Session()
compile_options = CompileOptions(session)
expr, log = Expression.compile('//*', compile_options)
if expr is None:
raise Exception("Error creating xpath expression", log)
runtime_options = RuntimeOptions(session)
runtime_options.initial_context = NodeItem.create_from_instance(xml.Instance.create_from_buffer(b'<?xml version="1.0"?><foo><bar>baz</bar></foo>')[0], session)
seq, log = expr.execute(runtime_options)
if seq is None:
raise Exception("Error executing xpath expression", log)
for i in seq:
print(i.informationItem().local_name)
Base class: xpath.Executable
-> xpath.Module
- class xpath.Expression¶
Class methods¶
- classmethod xpath.Expression.compile(type cls, unicode text, CompileOptions options)¶
- Compiles a new
xpath.Expression
from the provided expression text andxpath.CompileOptions
. This involves syntax checking, building of AST, initializing of static context components and static-analysis. In case of a syntax or a static error, the
xpath.Expression
is None and the xml.ErrorLog contains the error(s).Returns: (
xpath.Expression
, xml.ErrorLog)
s = Session() expr, log = Expression.compile('year-from-date(current-date()) ge 2018', CompileOptions(s)) if expr None: print(log) else: print('success') True
- Compiles a new
Attributes¶
- xpath.Module.location
- xpath.Executable.referenced_modules
- xpath.Module.schema
- xpath.Module.strip_input_type_annotations
- xpath.Module.whitespace_handler
Methods¶
- xpath.Expression.execute(RuntimeOptions options)¶
- Execute the compiled
xpath.Expression
with the providedxpath.RuntimeOptions
. In case of a runtime error the returned
xpath.Sequence
is None and the xml.ErrorLog contains the error.Returns: (
xpath.Sequence
, xml.ErrorLog)
s = Session() expr, log = Expression.compile('year-from-date(current-date()) ge 2018', CompileOptions(s)) seq, log = expr.execute(RuntimeOptions(s)) seq[0].anySimpleType().value True
- Execute the compiled
- xpath.Module.load_xml_document(unicode uri, f)
Special methods¶
__eq__, __ge__, __gt__, __le__, __lt__, __ne__