Dealing with xml namespaces with ElementTree

Neil Cerutti neilc at norwich.edu
Fri Jan 21 09:21:46 EST 2011


I have to parse many xml documents that senselessly(?) specify a
single namespace for the whole document. After a couple of years,
my approach has boiled down to the following three little
helpers, for use with ElementTree:
def insert_namespace(xpath):
 # Enable *simple* xpath searches by inserting the fscking namespace.
 return '/'.join('{{{}}}{}'.format(XMLNS, n) for n in xpath.split('/'))
def find(et, xpath):
 return et.find(insert_namespace(xpath))
def findall(et, xpath):
 return et.findall(insert_namespace(xpath))
Instead of writing, e.g.,
et.find('{{0}}ab/{{0}}cd'.format(XMLNS), et al, I can use
find(et, 'ab/cd').
Is there a better ElemenTree based approach I'm missing out on?
And on the other hand, am I circumventing something important, or
inviting bad limitations of some kind?
-- 
Neil Cerutti


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /