Package genshi :: Module path :: Class Path

Class Path

object --+
         |
        Path

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.

Instance Methods
 
__init__(self, text, filename=None, lineno=-1)
Create the path object from a string.
 
__repr__(self)
repr(x)
Stream
select(self, stream, namespaces=None, variables=None)
Returns a substream of the given stream that matches the path.
function
test(self, ignore_context=False)
Returns a function that can be used to track whether the path matches a specific stream event.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables
  STRATEGIES = (<class 'genshi.path.SingleStepStrategy'>, <class...
Properties

Inherited from object: __class__

Method Details

__init__(self, text, filename=None, lineno=-1)
(Constructor)

 
Create the path object from a string.
Parameters:
  • text - the path expression
  • filename - the name of the file in which the path expression was found (used in error messages)
  • lineno - the line on which the expression was found
Overrides: object.__init__

__repr__(self)
(Representation operator)

 
repr(x)
Overrides: object.__repr__
(inherited documentation)

select(self, 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: Stream
the substream matching the path, or an empty stream

test(self, 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 and variables. 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 an updateonly keyword argument that default to False. If it is set to True, 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: function
a function that can be used to test individual events in a stream against the path

Class Variable Details

STRATEGIES

Value:
(<class 'genshi.path.SingleStepStrategy'>,
 <class 'genshi.path.SimplePathStrategy'>,
 <class 'genshi.path.GenericStrategy'>)