diff options
| author | Stefan Behnel <stefan_ml@behnel.de> | 2011-10-27 09:44:29 +0200 |
|---|---|---|
| committer | Stefan Behnel <stefan_ml@behnel.de> | 2011-10-27 09:44:29 +0200 |
| commit | 92fb3bcc1b607d94fcd294033d062f19cf4e376b (patch) | |
| tree | d83ade26e6a4666b196d0ded8668a4320e8d2b78 /doc | |
| parent | 1e7c830d97166cb39498cc5c1fe05a6ec7844736 (diff) | |
| download | python-lxml-92fb3bcc1b607d94fcd294033d062f19cf4e376b.tar.gz | |
extended doctests
--HG--
extra : rebase_source : 766165283e9ec038d42834d407d84c5066c3b716
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/element_classes.txt | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/doc/element_classes.txt b/doc/element_classes.txt index 0e2e3fde..a26bec05 100644 --- a/doc/element_classes.txt +++ b/doc/element_classes.txt @@ -16,9 +16,9 @@ provides the Element interface for subclasses: >>> from lxml import etree >>> class honk(etree.ElementBase): + ... @property ... def honking(self): ... return self.get('honking') == 'true' - ... honking = property(honking) This defines a new Element class ``honk`` with a property ``honking``. @@ -294,9 +294,26 @@ value in a dictionary. It is set up as follows: >>> parser = etree.XMLParser() >>> parser.set_element_class_lookup(lookup) -This class uses its fallback if the attribute is not found or its value is not -in the mapping. Normally, the default class lookup is used here. If you want -to use the namespace lookup, for example, you can use this code: +And here is how to use it: + +.. sourcecode:: pycon + + >>> xml = '<a id="123"><b id="1234"/><b id="1234" honking="true"/></a>' + >>> a = etree.fromstring(xml, parser) + + >>> a.honking # id does not match ! + Traceback (most recent call last): + AttributeError: 'lxml.etree._Element' object has no attribute 'honking' + + >>> a[0].honking + False + >>> a[1].honking + True + +This lookup scheme uses its fallback if the attribute is not found or +its value is not in the mapping. Normally, the default class lookup +is used here. If you want to use the namespace lookup, for example, +you can use this code: .. sourcecode:: pycon @@ -502,9 +519,9 @@ can just pass None as an element name: >>> namespace[None] = HonkNSElement # default Element for namespace >>> class HonkElement(HonkNSElement): + ... @property ... def honking(self): ... return self.get('honking') == 'true' - ... honking = property(honking) >>> namespace['honk'] = HonkElement # Element for specific tag Now you can rely on lxml to always return objects of type HonkNSElement or its |
