开发者

lxml version problem - unable to call fndall method !

开发者 https://www.devze.com 2023-01-09 05:20 出处:网络
lxml gives following error on version 1.3 for the below line.. self.doc.findall(\'.//field[@on_change]\')

lxml gives following error on version 1.3 for the below line..

self.doc.findall('.//field[@on_change]')


File "/home/.../code_generator/xmlGenerator.py", line 158, in processOnChange
onchangeNodes = self.doc.findall('.//field[@on_change]')
File "etree.pyx", line 1042, in etree._Element.findall
File "/usr/lib/python2.5/site-packages/lxml/_elementpath.py", line 193, in findall
return _compile(path).findall(element)
File "/usr/lib/python2.5/site-packages/lxml/_elementpath.py", line 171, in _compile
p = Path(path)
File "/usr/lib/python2.5/site-packages/lxml/_elementpath.py", line 88, in __init__
"expected path separator (%s)" % (op or tag)
SyntaxError: expected path separator ([)

It works perfectly on local machine having lxml = 2.1.

my question is whats alternative for it, I tried to update the server's lxml version but 开发者_StackOverflow社区failed to do that as operating system is gusty - ubuntu 7.10 related post


Predicates in ElementPath expressions were only added in a later version. The original (c)ElementTree module (included in stdlib) has this only as of version 1.3 (in stdlib python 2.7). Lxml started using ElementTree 1.3 compatible expressions from version 2.0 on I think (when ElementTree 1.3 was still alpha)

The easiest solution: use the xpath() method, which can use real xpath expressions instead of only the subset that ElementPath supports (the lxml faq explains why they have the both xpath() and findall())

self.doc.xpath('.//field[@on_change]')

or filter on the attribute yourself (in case you would want something that works with the stdlib ElementTree as well):

[i for i in self.doc.findall('.//field') if i.get('on_change') is not None]
0

精彩评论

暂无评论...
验证码 换一张
取 消