xml.etree.ElementTree and XPath

X

xkenneth

All,

Can I execute XPath queries on ElementTree objects ignoring the
namespace? IE './node' instead of './{http://namespace.com}node'.

Is there any support for XPath and Minidom?

Regards,
Ken
 
S

Stefan Behnel

xkenneth said:
Can I execute XPath queries on ElementTree objects ignoring the
namespace? IE './node' instead of './{http://namespace.com}node'.

The XPath support in ET is very limited. You can use lxml.etree instead, which
has full support for XPath 1.0, i.e. you can do

tree.xpath('//*[local-name() = "node"]')

http://codespeak.net/lxml/

Or you can do the iteration yourself, i.e.

for el in tree.iter(): # or tree.getiterator():
if isinstance(el.tag, basestring):
if el.tag.split('}', 1)[-1] == "node":
print el.tag

which works in both ET and lxml.etree.

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top