Findall equivalent for xpath , Lxml

I am extracting text with respect to tags and I need to get them in a list form w.r.t p tags. I have this xpath expression as:

 find =  etree.XPath("//w:p//.//*[local-name() = 'ins']//text()" ,namespaces={'w':"http://schemas.openxmlformats.org/wordprocessingml/2006/main"}) 

And i want to use it in a findall expression. I tried:

inserted_list_1=[]
for p in lxml_tree.findall('.//{' + w + '}p'):
    inserted_list_1.append([t.text for t in p.findall('.//{' + w + '}ins')])

but all this returns is a list full of None values whilst the former xpath works perfectly. I think there's some intermediate path missing.

Answers


You cannot use that expression with findall(); the findall() method deliberately keeps compatibility with the limited ElementTree API XPath support.

Use the xpath() method instead:

for p in lxml_tree.xpath('.//w:p', namespaces={'w': w}):

and just use namespace prefixes for much more readable queries.

If you just wanted to extract all contained text, you can use:

[t for t in p.xpath('../w:p//w:ins//text()',namespaces={'w': w})]

Need Your Help

Using forms to reload and update a PHP page with jQUERY/AJAX

php jquery html ajax

I want to post info to a PHP page from html inputs and reload it dynamically using jQUERY and AJAX. The function should take all the values from the page inputs and post them to the PHP page, then ...

Memory issue when storing images in byteArray

java android performance

I have an app that needs to access a large number of images very quickly, so I need to load those images into memory in some way. Doing so as bitmaps used over 100MB of RAM, which was completely o...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.