lxml.pyclasslookup

Package lxml :: Module pyclasslookup |
[frames] | no frames]

# Module pyclasslookup

A whole-tree Element class lookup scheme for lxml.etree.

This class lookup scheme allows access to the entire XML tree in read-only mode. To use it, let a class inherit from PythonElementClassLookup and re-implement the `lookup(self, doc, root)` method:

>     >>> from lxml import etree, pyclasslookup
>     >>>
>     >>> class MyElementClass(etree.ElementBase):
>     ...     honkey = True
>     ...
>     >>> class MyLookup(pyclasslookup.PythonElementClassLookup):
>     ...     def lookup(self, doc, root):
>     ...         if root.tag == "sometag":
>     ...             return MyElementClass
>     ...         else:
>     ...             for child in root:
>     ...                 if child.tag == "someothertag":
>     ...                     return MyElementClass
>     ...         # delegate to default
>     ...         return None

Note that the API of the Element objects is not complete. It is purely read-only and does not support all features of the normal lxml.etree API (such as XPath, extended slicing or some iteration methods).

Also, you cannot wrap such a read-only Element in an ElementTree, and you must take care not to keep a reference to them outside of the `lookup()` method.

See http://codespeak.net/lxml/element_classes.html

Version: 2.0.4-53745

Classes
| PythonElementClassLookup PythonElementClassLookup(self, fallback=None) Element class lookup based on a subclass method.