rewrite ElementTree code in lxml

跟風遠走 提交于 2019-12-24 20:27:21

问题


I am writing a code to extract text from a xml file using ElementTree but I found out that lxml is giving xpath features which is more convenient. So i want to know how this line could be rewritten in lxml

if x.nodeName == 'a:pPr' and x.getAttribute('lvl') == '2' and x.hasAttribute('marL') == False:

currently I am suggested to use this..

'/p:sld/p:cSld/p:spTree/p:sp/p:nvSpPr/p:nvPr/x[@type="body" and  @sz="quarter"  and  @marL]'

Hope my question is clear!


回答1:


I'm assuming you are already at a stage where you're ready to use an xpath. If not http://lxml.de/parsing.html should help. The xpath for finding the element, you are searching for in your first line should look like this:

"//a:pPr[@lvl=2 and not(@marL)]"

I however have to admit that I have no idea how your second snippet should find that element O_o



来源:https://stackoverflow.com/questions/16912361/rewrite-elementtree-code-in-lxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!