|Version 4 (modified by cmlenz, 8 years ago)|
Using XPath in Genshi
Genshi provides basic XPath support for matching and querying event streams.
Due to the streaming nature of the processing model, Genshi uses only a subset of the XPath 1.0 language.
In particular, only the following axes are supported:
This means you can't use the parent, ancestor, or sibling axes in Genshi (the namespace axis isn't supported either, but what you'd ever need that for I don't know). Basically, any path expression that would require buffering of the stream is not supported.
Predicates are of course supported, but path expressions inside predicates are restricted to attribute lookups (again due to the lack of buffering).
Most of the XPath functions and operators are supported, however they (currently) only work inside predicates. The following functions are not supported:
The mathematical operators (+, -, *, div, and mod) are not yet supported, whereas sub-expressions and the various comparison and logical operators should work as expected.
You can also use XPath variable references ($var) inside predicates.
The Stream class provides a select(path) function that can be used to retrieve subsets of the stream:
>>> from genshi.input import XML >>> doc = XML('''<doc> ... <items count="4"> ... <item status="new"> ... <summary>Foo</summary> ... </item> ... <item status="closed"> ... <summary>Bar</summary> ... </item> ... <item status="closed" resolution="invalid"> ... <summary>Baz</summary> ... </item> ... <item status="closed" resolution="fixed"> ... <summary>Waz</summary> ... </item> ... </items> ... </doc>''') >>> print(doc.select('items/item[@status="closed" and ' ... '(@resolution="invalid" or not(@resolution))]/summary/text()')) BarBaz