问题
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