2010-07-27 65 views
0

LXML給出了1.3版本,下面的線下面的錯誤..lxml版本問題 - 無法調用fndall方法!

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 ([) 

它完美地在具有LXML = 2.1的本地機器。

我的問題是什麼替代它,我試圖更新服務器的lxml的版本,但沒有做,因爲操作系統是風口 - Ubuntu的7.10 related post

回答

3

謂詞ElementPath表達;在以後的版本只增加。原來的(c)ElementTree模塊(包含在stdlib中)只有1.3版本(在stdlib python 2.7中)。 LXML使用上,我認爲(在ElementTree的1.3仍然阿爾法)從2.0版本的ElementTree 1.3兼容的表情開始

最簡單的解決辦法:使用xpath()方法,該方法可以使用,而不是隻子集真正的XPath表達式ElementPath支持(該lxml faq解釋了爲什麼他們有兩個xpath()findall())的屬性自己

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

或過濾器(如果你想要的東西,與STDLIB ElementTree的協同工作):

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