genshi.path¶
Basic support for evaluating XPath expressions against streams.
>>> 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
Because the XPath engine operates on markup streams (as opposed to tree structures), it only implements a subset of the full XPath 1.0 language.
-
class
genshi.path.
Path
(text, filename=None, lineno=-1)¶ Implements basic XPath support on streams.
Instances of this class represent a “compiled” XPath expression, and provide methods for testing the path against a stream, as well as extracting a substream matching that path.
-
select
(stream, namespaces=None, variables=None)¶ Returns a substream of the given stream that matches the path.
If there are no matches, this method returns an empty stream.
>>> from genshi.input import XML >>> xml = XML('<root><elem><child>Text</child></elem></root>')
>>> print(Path('.//child').select(xml)) <child>Text</child>
>>> print(Path('.//child/text()').select(xml)) Text
Parameters: - stream – the stream to select from
- namespaces – (optional) a mapping of namespace prefixes to URIs
- variables – (optional) a mapping of variable names to values
Returns: the substream matching the path, or an empty stream
Return type: Stream
-
test
(ignore_context=False)¶ Returns a function that can be used to track whether the path matches a specific stream event.
The function returned expects the positional arguments
event
,namespaces
andvariables
. The first is a stream event, while the latter two are a mapping of namespace prefixes to URIs, and a mapping of variable names to values, respectively. In addition, the function accepts anupdateonly
keyword argument that default toFalse
. If it is set toTrue
, the function only updates its internal state, but does not perform any tests or return a result.If the path matches the event, the function returns the match (for example, a START or TEXT event.) Otherwise, it returns
None
.>>> from genshi.input import XML >>> xml = XML('<root><elem><child id="1"/></elem><child id="2"/></root>') >>> test = Path('child').test() >>> namespaces, variables = {}, {} >>> for event in xml: ... if test(event, namespaces, variables): ... print('%s %r' % (event[0], event[1])) START (QName('child'), Attrs([(QName('id'), u'2')]))
Parameters: ignore_context – if True, the path is interpreted like a pattern in XSLT, meaning for example that it will match at any depth Returns: a function that can be used to test individual events in a stream against the path Return type: function
-
-
exception
genshi.path.
PathSyntaxError
(message, filename=None, lineno=-1, offset=-1)¶ Exception raised when an XPath expression is syntactically incorrect.