[lxml-dev] Element children not right?
Stefan Behnel
stefan_ml at behnel.de
Sat Jul 14 13:17:24 CEST 2007
Mike Meyer schrieb:
> I think I have a very, very basic bug here:
>
>>>> from sys import version
>>>> version
> '2.5.1 (r251:54863, May 15 2007, 15:31:37) \n[GCC 3.4.6 [FreeBSD] 20060305]'
>>>> from lxml import etree
>>>> etree.LXML_VERSION
> (1, 3, 2, 0)
>>>> etree.LIBXML_VERSION
> (2, 6, 29)
>>>> etree.LIBXSLT_VERSION
> (1, 1, 21)
>>>> d = etree.parse('/home/mwm/.plpwmrc.xml')
>>>> n = d.find('namemenu')
>>>> [el for el in n]
> [<Element getapp at 7f1e10>, <Element getapp at 8aa418>, <!-- Damn Firefox doesn't set the title properly when running tabbed, just
> uses "firefox-bin". So we try both cases. -->, <Element getapp at 8aa470>, <Element getapp at 8aa4c8>, <Element getapp at 926ec0>, <Element getapp at 926f18>, <Element getapp at 926f70>, <Element getapp at 926fc8>, <Element run at 977b50>, <Element getapp at 97d4c8>]
>>>> from xml.etree.ElementTree import parse
>>>> d2 = parse('/home/mwm/.plpwmrc.xml')
>>>> n2 = d2.find('namemenu')
>>>> [el for el in n2]
> [<Element getapp at 9845a8>, <Element getapp at 9845f0>, <Element getapp at 984638>, <Element getapp at 984680>, <Element getapp at 9846c8>, <Element getapp at 984710>, <Element getapp at 984758>, <Element getapp at 9847a0>, <Element run at 9847e8>, <Element getapp at 984830>]
>
> Note that the original ElementTree implementation only returns
> children that are *elements*, whereas the lxml version returns all
> the children. This makes life much more interesting, especially as
> there isn't an obvious method for checking whether or not the node
> value is actually an element.
Sure there is, just check for the tag property being a string. lxml.etree is
compatible to ElementTree in that it returns the factory functions for
everything that does not have a tag (comments, PIs, entities).
>>> [el for el in n if isinstance(el.tag, basestring)]
should do what you want, in both lxml.etree and ElementTree.
I don't see why this should be a bug, it's just an extended tree model.
Comments are nothing to frown upon, they are as much part of the XML world as
element nodes, so why would you want to ignore them?
Stefan
More information about the lxml-dev
mailing list