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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to